Get the last argument of the previous command in the bash shell

11 August, 2005 - 12:34
Categories:

Working on the command line shell typically involves using the same argument of the previous command. In bash there is a trick to save you those boring keystrokes. When you use "!$" (called bang dollar-sign) in a command, bash will expand it to the last argument of the previous command. A simple example to clarify:

$> file /etc/fstab
/etc/fstab: ASCII text
$> ls -l !$
ls -l /etc/fstab
-rw-r--r--  1 root root 894 2005-08-03 15:12 /etc/fstab

As you see bash replaced the "!$" of the second command with "/etc/fstab", the last argument of the first command. First bash shows the expanded code (first line of the output) and then executes the command.

For more handy bash tricks like these you should visit the resource where I found this "bang dollar-sign trick" : http://www.deadman.org/bash.html.

Post new comment

The content of this field is kept private and will not be shown publicly.
  • No HTML tags allowed
  • Lines and paragraphs break automatically.

More information about formatting options