Friday, 8 January 2016

Mysterious Uses of (!) Symbol or Operator


Join over 95,000+ Linux Users in the TecMint CommunityClick Here to Subscribe   



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


If you have any questions or problems regarding this article and want help within 24 Hours?  Ask Now 
 Support TecMint: Did you find this tutorial helpful?. Please help to keep it alive by donating. Every cent counts! - Donate Now
Avishek Kumar
I am a major in computer science, love to research nix. I love to write codes and scripts, review distros, experiment Foss Technologies, write technical articles, Hack, of course Ethically. I am working as System Administrator (nix) for a NGO.
Your name can also be listed here. Got a tip? Submit it here to become an TecMint author.
Receive Your Free Complimentary eBook NOW! - Linux Kernel In A Nutshell - Configuration and Building

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
    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
    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
      Dear Stephen,
      Thanks for the wonderful piece of Information. Keep connected and keep us aware of such context.
  3. suzy
    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
      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
    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
      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
    Nice Article
  6. Tachyon
    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.

LEAVE A REPLY




No comments:

Post a Comment