6

I looked at this question and googled, but can't find a definitive answer. What does !$ do when typed into terminal?

I typed

mkdir /path/to/dir
cd !$

Which brought me to /path/to/dir but I still would like to know exactly what the !$ operator does in general.

myol
  • 313

1 Answers1

8

From Bash Manual > History Expansion:

  • Event Designator

    !      Start a history substitution, except when followed by 
           a blank, newline, carriage return, = or  (  (when the 
           extglob shell option is enabled using the shopt builtin).
    !n     Refer to command line n.
    
  • Word Designators

    $      The  last word.  This is usually the last argument, but will 
           expand to the zeroth word if there is only one word in the line.
    

So, !$ just repeats the last word from previous command

Example:

$ echo Unix and Linux
Unix and Linux
$ echo !$
echo Linux
Linux

$ cat dir1
cat: dir1: Is a directory
$ cd !$
cd dir1

After executing command with !$, press and you will get the result of !$.

Pandya
  • 24,618
  • Oh! I found the question is duplicate of http://unix.stackexchange.com/questions/88642/what-does-mean (flagged to close) – Pandya Jan 11 '16 at 10:23