Stupid Bash Tricks: Repeat Last Argument to Last Command

Did you know that you can recall the last argument to the last command you typed by using the special variable !$. This can be handy when you are working with host names, long file names, all sorts of things. A few examples:

See if a host is up and log in:

usr-3@srv-3 usr-3]$ ping e2500.prod.bigfatdomain.com
(output of ping snipped -- you've seen it before!)
[usr-3@srv-3 usr-3]$ ssh !$
ssh e2500.prod.bigfatdomain.com
usr-3@e2500.prod.bigfatdomain.com's password:

User Names:

[root@srv-3 usr-3]# useradd -u 666 -g 100 udergahad
[root@srv-3 usr-3]# passwd !$
passwd udergahad
Changing password for user udergahad.
New UNIX password:

File Operations:

[root@srv-3 usr-3]# ls -l /var/log/messages
-rw-------    1 root    root     5490 Jun 24 09:20 /var/log/messages
[root@srv-3 usr-3]# cp !$ !$.020624
cp /var/log/messages /var/log/messages.020624
[root@srv-3 usr-3]# scp !$ atom:/share/usr-3/
scp /var/log/messages.020624 atom:/share/usr-3/
root@atom's password:
messages.020624   100% |*********************|  5490     00:00
[root@srv-3 usr-3]#

Those are just some dumb examples, but you’ll get a lot of bang from your buck with this trick.