Skip to main content

Starting with a Bang

You don't use a linux system for long without learning about !!. It's handy in situations like this:

$ echo "I'm king of the world!" >/etc/fstab
sudo echo "I'm king of the world!" >/etc/fstab

But history expansion can do much more than repeat the last command.

You can retrieve any command from your history:

$ history
502 echo "I'm king of the world"
503 ls
504 touch yesyouare
505 echo "I'm king of the world"
506 ls
507 cd /etc
$ !-3 # !! is the last command, !-2 is the command before that, and so on
echo "I'm king of the world"
I'm king of the world
$ sudo !504
sudo touch yesyouare

But it's often easier to search for the last command that started with a given string:

$ !echo
echo "I'm king of the world"
I'm king of the world
$ !v
vim ~/reasons_I_need_a_psychiatrist

These are simple string substitutions - bash replaces !! with the characters of your last command, then executes the resulting command. So with !! and the numerical operators you can do stuff like:

$ vim dave
dave's not here
$ ls
$ ! # ! would look for a command beginning with ''

You can also extract parts of commands! The one I use most is !$ for the last argument:

$ ls some/godawful/long/path/probably/from/a/java/project
$ vim !$/file.txt
vim some/godawful/long/path/probably/from/a/java/project/file.txt

Note that we can combine this with the line selectors to get the last argument of an earlier command:

$ curl
[file dumped to console]
$ man curl
$ curl -O !-2$
curl -O

You can also get all the arguments, or just the first one:

$ touch foo bar baz # whoops, I'm in the wrong directory
$ mv !* projects/foobar
mv foo bar baz projects/foobar
$ cp /boot/initramfs-linux.img ~/backupdir
$ sudo vim !^
sudo vim /boot/initramfs-linux.img # I've got a GREAT idea!

There's more, but I'll stop here for now.

You can go pretty far with only a very basic understanding of bash, but shells are actually very interesting tools that will help you more if you give them a chance. If you spend a lot of time on the command line, it's well worth the learning curve.


Comments powered by Disqus