An amazing exclamation point (!) in the Linux Command Line (!)

Source: Internet
Author: User

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.

  1. $ 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.

  1. $ !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.

  1. $ history
  2. $ !-6
  3. $ !-8
  4. $ !-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:

  1. $ 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:

  1. $ 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.

  1. $ touch /home/avi/Desktop/1.txt

Then copy it to the cp command using the absolute path/home/avi/Downloads.

  1. $ 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.

  1. $ echo "1st Argument is : !^"
  2. $ 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:

  • 1
  • 2
  • Next Page

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.