10 Amazing and Mysterious Uses of (!) Symbol or Operator in Linux Commands

If You Appreciate What We Do Here On TecMint, You Should Consider:

  1. Stay Connected to: Twitter | Facebook | Google Plus
  2. Subscribe to our email updates: Sign Up Now
  3. Use our Linode referral link if you plan to buy VPS (it starts at only $10/month).
  4. Support us via PayPal donate - Make a Donation
  5. Support us by purchasing our premium books in PDF format.
  6. Support us by taking our online Linux course in 5 Days

We are thankful for your never ending support.

Receive Your Free Complimentary eBook NOW! -

Download Free Linux eBooks

Advanced Bash-Scripting Guide
Linux Bible
A Newbie's Getting Started Guide to Linux
Introduction to Linux - A Hands on Guide

You may also like...

12 Responses

  1. Edgar Allen says:

    You might also mention !?

    It finds the last command with its’ string argument. For example, if…
    1013 grep tornado /usr/share/dict/words
    1014 grep hurricane /usr/share/dict/words
    1015 wc -l /usr/share/dict/words

    are all in the history then !?torn will grep for tornado again where !torn would search in vain for a command starting with torn.

    And `wc !?torn?:2` works to select argument two from the command containing tornado and run `wc` on it.

  2. Stephen says:

    I didn’t see a mention of historical context in the article, so I’ll give some here in the comments. This form of history command substitution originated with the C Shell (csh), created by Bill Joy for the BSD flavor of UNIX back in the late 70’s. It was later carried into tcsh, and bash (Bourne-Again SHell).

    Personally, I’ve always preferred the C-shell history substitution mechanism, and never really took to the fc command (that I first encountered in the Korne shell).

    • Avishek Kumar says:

      Dear Stephen,
      Thanks for the wonderful piece of Information. Keep connected and keep us aware of such context.

  3. suzy says:

    4th command. You can access it much simpler. There are actually regular expressions:
    ^ is at the begging expression
    $ is at the end expression
    :number any number parameter

    Example:
    touch a.txt b.txt c.txt
    echo !^ –> display first parameter
    echo !:1 –> also display first parameter
    echo !:2 –> display second parameter
    echo !:3 –> display third parameter
    echo !$ –> display last (in our case 3th) parameter
    echo !* –> display all parameters

    • Avishek Kumar says:

      And we did the same

      echo “1st Argument is : !^”
      $ echo “2nd Argument is : !cp:2”

      echo followed by 1st argument and 2nd argument is just to make the tutorial and command understandable. I have used ‘!^’. ‘!command:number’ as you are suggesting.

      I would like to know if you mean something different?

  4. Tomasz Wiszkowski says:

    I think (5) works differently than you pointed out, and redirection to devnull hides it, but ZSh still prints the command.
    When you invoke “! ls…”, it always picks the last ls command you executed, just appends your switches at the end (after /dev/null).

    One extra cool thing is the !# operator, which picks arguments from current line. Particularly good if you need to retype long path names you already typed in current line. Just say, for example

    cp /some/long/path/to/file.abc !#:1

    And press tab. It’s going to replace last argument with entire path and file name.

    • Avishek Kumar says:

      Tomasz,

      For your first part of feedback: It doesn’t pick the last command executed and just to prove this we have used 4 different switches for same command. ($ ! ls $ ! ls -l $ ! ls -la $ ! ls -lA ). Now you may check it by entering the keywords in any order and in each case it will output the same result.

      As far as it is not working in ZSH as expected, i have already mentioned that it i have tested it on BASH and most of these won’t work in other shell.

      For the second part, what you mentioned is a HASH TAG in Linux Command Line and we have included it in one of our article. You may like to read it here: http://www.tecmint.com/linux-commandline-chat-server-and-remove-unwanted-packages/

  5. Manu says:

    Nice Article

  6. Tachyon says:

    Great post. Thanks, I’ve reshared it to G+

    One note, “su” stands for “switch user” not “Suitable User”. Minor note.
    At least that’s still representative of what it actually does, unlike when most people refer to it as “Super User”, thinking it only grants root user status, which is utterly incorrect.

Got something to say? Join the discussion.

Your email address will not be published. Required fields are marked *

Join Over 95000+ Linux Users
  1. 77,543
  2. 4,213
  3. 35,218

Enter your email to get latest Linux Howto's