40

Bang dollar seems to refer to the last part of the last command line.

E.g.

$ ls -l
 .... something
$ !$
-l
bash: -l command not found

I can find plenty on the dollar variables (e.g. $!) but not on this. Any explanation?

gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104
Snowcrash
  • 80,579
  • 89
  • 266
  • 376

3 Answers3

66

That's the last argument of the previous command. From the documentation:

!!:$

designates the last argument of the preceding command. This may be shortened to !$.

Remark. If you want to play around with Bash's history, I suggest you turn on the shell option histverify like so:

shopt -s histverify

(you can also put it in your .bashrc to have it on permanently). When using history substitution, the substitution is not executed immediately; instead, it is put in readline's buffer, waiting for you to press enter… or not!


To make things precise, typing !$ is not equivalent to typing "$_": !$ is really a history substitution, refering to the last word of the previous command that was entered, whereas "$_" is the last argument of the previously executed command. You can compare both (I have shopt -s histverify):

$ { echo zee; }
zee
$ echo "$_"
zee
$ { echo zee; }
zee
$ echo !$
$ echo }

Also:

$ if true; then echo one; else echo two; fi
one
$ echo "$_"
one
$ if true; then echo one; else echo two; fi
$ echo !$
$ echo fi

And also:

$ echo zee; echo "$_"
zee
zee
$ echo zee2; echo !$
$ echo zee2; echo "$_"

And also

$ echo {1..3}
1 2 3
$ echo "$_"
3
$ echo {1..3}
1 2 3
$ echo !$
$ echo {1..3}

And also

$ echo one ;
$ echo "$_"
one
$ echo one ;
one
$ echo !$
$ echo ;

There are lots of other examples, e.g., with aliases.

gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104
10

!$ can do what $_ does, except the fact that $_ does not store the value it returns (as its substitution) to history.

Here is an example.

With !$

za:tmep za$ ls -lad 
drwxr-xr-x  4 za  staff  136 Apr  6  2016 .
za:tmep za$ !$
-lad
-bash: -lad: command not found
za:tmep za$ history | tail -n 3
  660  ls -lad 
  661  -lad                     <<== history shows !$ substitution.  
  662  history | tail -n 3

With $_

za:tmep za$ ls -lad
drwxr-xr-x  4 za  staff  136 Apr  6  2016 .
za:tmep za$ $_
-bash: -lad: command not found
za:tmep za$ history | tail -n 3
  663  ls -lad
  664  $_         <<== history shows $_ and not its substitution. 
  665  history | tail -n 3
za:tmep za$ 

More options:

!^      first argument
!:2     second argument
!:2-$   second to last arguments
!:2*    second to last arguments
!:2-    second to next to last arguments
!:2-3   second to third arguments
!$      last argument
!*      all arguments
SherylHohman
  • 16,580
  • 17
  • 88
  • 94
z atef
  • 7,138
  • 3
  • 55
  • 50
  • 6
    Technically, `!$` and `$_` are **not** the same. `!$` acts on a higher level. You can see the differences with aliases, or with compound commands, or with multiple commands. Compare `echo !$` with `echo $_` in the following cases: 1. `{ echo zee; }`; 2. `if true; then echo one; else echo two; fi`; 3. Also type two commands, e.g., `echo zee; echo $_` and `echo zee2; echo $!`. – gniourf_gniourf Dec 29 '16 at 19:06
1

Monkey's answer:

whit !$ you can easily print the last word of the previous command

#Create new file
touch newfile.txt
#Edit new file using !$ instead newfile.txt again
nano !$
Mike D3ViD Tyson
  • 1,701
  • 1
  • 21
  • 31