Home >Operation and Maintenance >Nginx >Eight mysterious uses of the '!' operator in Linux commands
In different shells, the usage of most Linux commands using the '!' symbol may vary. While the examples I provide are typically used in bash shells, some other Linux shells may have different implementations or may not support certain uses of the '!' symbol at all.
Let’s dive into the surprising and mysterious uses of the ‘!’ symbol in Linux commands.
A useful tip is that you can run a command from a previously executed Find the historical command in the command and run it again. First, find the number of the command by running the 'history' command.
linuxmi@linuxmi:~/www.linuxmi.com$ history
Find recently executed commands in Linux
To run a command from history by command number, you can use the '!' symbol followed by the command number ,As follows.
$ !58
Run command by command number
When you execute the above command, it will run the command at line 58 in the history.
Please note that the actual command number may vary depending on your command history. You can use the history command to view a list of commands and their line numbers.
You can run previously executed commands in the order in which they are run. The last command run will be expressed as -1, the second to last is -2, the seventh to last is -7, and so on. You can use !-n, where n is the reciprocal number of the command you want to quote. As shown below
$ history$ !-3$ !-6$ !-10
Rerun the command in Linux
I needed to list the contents of the directory '/home/linuxmi/snap', so I executed the following command:
$ ls /home/linuxmi/snap
Then I realized that I should run the "ls -l" command to see which files Executable. Instead of retyping the entire command, just pass the parameters of the previous command to this new command, like this:
$ ls -l !$
Here, '!$' will be The parameters passed in the previous command are passed to this new command.
Suppose I create a text file named 1.txt on the desktop.
linuxmi@linuxmi ~/www.linuxmi.com% touch /home/linuxmi/linuxmi.go
Then copy it to the '/home/avi/Downloads' directory using the full path, using the cp command.
linuxmi@linuxmi ~/www.linuxmi.com% cp /home/linuxmi/linuxmi.go /home/linuxmi/go
Now we pass two parameters in the cp command. The first one is '/home/avi/Desktop/1.txt' and the second one is '/home/avi/Downloads'. We can print two parameters in different ways by executing the echo command and using different parameters.
linuxmi@linuxmi ~/www.linuxmi.com% echo "第一个参数是:!^"echo "第一个参数是:/home/linuxmi/linuxmi.go"第一个参数是:/home/linuxmi/linuxmi.golinuxmi@linuxmi ~/www.linuxmi.com% echo "第二个参数是:!cp:2"echo "第二个参数是:/home/linuxmi/go"第二个参数是:/home/linuxmi/go
Please note that the first parameter can be displayed as "!^", while other parameters can be printed by executing "![Command Name]:[Parameter Number]".
In the above example, the first command is 'cp' and the second parameter needs to be printed. Hence "!cp:2". For the xyz command with 5 parameters, if you need to get the 4th parameter, you can use "!xyz:4" and use that parameter as needed. All parameters can be accessed via "!*".
Processing two or more parameters
We can execute recently executed commands based on keywords. The details are as follows:
$ ls /home > /dev/null [Command 1]$ ls -l /home/linuxmi/linuxmi > /dev/null [Command 2] $ ls -la /home/linuxmi/linuxmi.com > /dev/null [Command 3]$ ls -lA /usr/bin > /dev/null [Command 4]
Here we use the ls command, but with different options and different folders. Also, to keep the console clean, we will send the output of each command to "/dev/null".
Now execute the last executed command based on the keyword:
$ ! ls [Command 1]$ ! ls -l [Command 2] $ ! ls -la [Command 3]$ ! ls -lA [Command 4]
检查输出,你会惊讶地发现你正在运行已经执行过的命令,只是使用了ls关键词。
你可以使用(!!)操作符来运行/修改你上次执行的命令,这是一个简写符号,允许你引用在命令行中执行的上一个命令。
例如,我运行了一个单行脚本来查找Linux机器的IP地址:
$ ip addr show | grep inet | grep -v 'inet6'| grep -v '127.0.0.1' | awk '{print $2}' | cut -f1 -d/
然后突然我发现我需要将上述脚本的输出重定向到一个名为ip.txt的文件中,那么我该怎么办呢?我需要重新输入整个命令并将输出重定向到文件吗?好吧,一个简单的解决方案是使用上箭头键来调出上一条命令,并在末尾添加’> ip.txt’来将输出重定向到文件。
$ ip addr show | grep inet | grep -v 'inet6'| grep -v '127.0.0.1' | awk '{print $2}' | cut -f1 -d/ > ip.txt
感谢上箭头键的救命作用。现在考虑以下情况,下次我运行下面的单行脚本。
ifconfig | grep "inet addr:" | awk '{print $2}' | grep -v '127.0.0.1' | cut -f2 -d:
当我运行脚本时,bash提示返回了一个错误,信息为“bash: ifconfig: command not found”,我很容易猜到我以一个普通用户的身份运行了这个命令,而它应该以root身份运行。
那么解决办法是什么呢?登录为root然后重新输入整个命令是很困难的!在上一个示例中的(上箭头键)在这里也无法帮助。所以,要调用用户的最后一个命令,需要输入“!!”(不需要引号)
su -c “!!” root
这里的su是切换用户的命令,root是要切换到的用户,-c是以指定的用户身份运行命令的选项,最重要的部分是!!将被替换为上次运行的命令。是的!你需要提供root密码。
在Linux中,’!’操作符(也称为”bang”操作符)用于历史扩展,它允许你引用先前的命令并对其执行各种操作。要从目录中删除除了特定文件(important_file.txt)之外的所有文件,可以使用带有’!’操作符的rm命令,如下所示。
$ rm !(important_file.txt)
要从文件夹中删除除了扩展名为’.pdf’之外的所有文件类型。
$ $ rm !(*.pdf)
在这里,我们将使用’! -d’来验证目录是否存在,如果目录不存在,则紧随其后的是逻辑与操作符(&&),打印出目录不存在,如果目录存在,则紧随其后的是逻辑或操作符(||),打印出目录存在。
逻辑是,当[ ! -d /home/linuxmi/linuxmi.com ]的输出为0时,它将执行逻辑与之后的内容,否则它将转到逻辑或(||)并执行逻辑或之后的内容。
$ [ ! -d /home/linuxmi/linuxmi.com ] && printf '\nno such /home/linuxmi/linuxmi.com directory exist\n' || printf '\n/home/linuxmi/linuxmi.com directory exist\n'
类似于上面的条件,但是如果所需目录不存在,它将退出命令。
$ [ ! -d /home/linuxmi/linuxmi.com] && exit
在脚本语言中的一般实现,如果所需目录不存在,它将创建一个目录。
[ ! -d /home/linuxmi/linuxmi.com] && mkdir /home/linuxmi/linuxmi.com
The above is the detailed content of Eight mysterious uses of the '!' operator in Linux commands. For more information, please follow other related articles on the PHP Chinese website!