An amazing exclamation point (!) in the Linux Command Line (!)
'!'In Linux, symbols can not only be used as negative symbols, but also be used to retrieve commands from historical Command records or run commands before execution without modification. All the following commands have been verified in Bash Shell. Although I have not tried it, most of them cannot run in other shells. Here we will introduce the symbols in the Linux Command Line.'!'Amazing and amazing usage.
1. Use a number to find a command from the history command list for execution.
You may not realize that you can find one in the History command list (previously executed command set) to run. First, run the "history" command to find the serial number of the previous command.
$ history
Use the history command to find the last command
Now, you only need to use the number displayed before the command in the History command output to run this command. For examplehistoryThe output command is 1551.
$ !1551
Use the command ID to run the last command.
In this way, the command numbered 1551 (the top command in the preceding example) runs. This method is very useful for executing previous commands by using the ID number, especially when these commands are very long. You only need to use it! You can call the [history command output sequence number.
2. Run the last, second, and seventh commands.
You can run the previously executed command in another way. Use-1 to represent the last command,-2 to represent the second to last, and-7 to represent the seventh to last command.
First, use the history command to obtain the list of executed commands. It is necessary to execute the history command because it can be used to ensure that norm command > fileOr other commands that may cause danger. Next, execute the sixth, eighth, and tenth commands.
$ history
$ !-6
$ !-8
$ !-10
Run the command before using the negative number
3. Pass the parameters of the last command to run the new command conveniently.
I need to display/home/$USER/Binary/firefoxFolder content, so I execute:
$ ls /home/$USER/Binary/firefox
Next, I realized that I should execute 'LS-l' to check which file is executable. So should I re-enter the entire command? No, I don't need it. I only need to add the final parameters in the new command, such:
$ ls -l !$
Here!$Pass the parameters of the last command to the new command.
Pass the parameters of the previous command to the new command.
4. How to use it! To process two or more parameters.
For example, I created a file file1.txt on the table.
$ touch /home/avi/Desktop/1.txt
Then copy it to the cp command using the absolute path/home/avi/Downloads.
$ cp /home/avi/Desktop/1.txt/home/avi/downloads
Here, we pass two parameters to the cp command. The first one is/home/avi/Desktop/1.txtAnd the second is/home/avi/Downloads. Let's process them separately and useEcho [parameters]To print two different parameters.
$ echo "1st Argument is : !^"
$ echo "2nd Argument is : !cp:2"
Note that the first parameter can be used"!^"Other commands can be printed through"! [Command name]: [parameter number]"Print.
In the preceding example, the first command iscpThe second parameter also needs to be printed. So yes"!cp:2"If any command, such as xyz, has five parameters, and you need to obtain the fourth parameter, you can use"!xyz:4". All parameters can be passed through"!*".
Process two or more parameters
For more details, please continue to read the highlights on the next page: