The bash !$ trick: Insert the last argument of the previous command in the current command

The !$ trick works on bash, csh, tcsh and zsh, but does not work on ksh.

The !$ trick is very usefull in the Linux and Unix shells.

In the Bash shell, the !$ inserts (and expands) the last used argument (from the previous command) in the current command:

$ ls -l /etc/passwd
-rw-r--r-- 1 root root 2579 2012-07-24 23:52 /etc/passwd
$ ls -l !$
ls -l /etc/passwd
-rw-r--r-- 1 root root 2579 2012-07-24 23:52 /etc/passwd

$ ls /etc/passwd
/etc/passwd
$ ls !$
ls /etc/passwd
/etc/passwd

In Bash, to insert the last used argument in the command line, you can also use $_ :

$ ls -l /etc/shadow
-rw-r----- 1 root shadow 2559 2012-07-24 23:52 /etc/shadow
$ ls -l $_
-rw-r----- 1 root shadow 2559 2012-07-24 23:52 /etc/shadow

The bash has the Alt-. key combination binded to insert the last argument of the previous command in the current one:

$ ls /etc/shadow
/etc/shadow
$ ls [Alt-.] # in this case, Alt-. expanded is /etc/shadow

Every bash user should masterize the !$ and the !! bash tricks. To read the other bash tricks, click here.

Liked it? Take a second to support Geekster on Patreon!
Tagged with: , , , , , , , , , , , ,
Posted in The Linux and Unix Articles!

Leave a Reply

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

*

Support LinuxG via Patreon
Support LinuxG.net on Patreon!
Subscribe

  

Subscribe to get the latest Linux news and how to guides directly on your e-mail!