Repeat of the last command in bash

Ok, we all know that to repeat the last command in bash (as well as many other shells) you just need to press the Up key. Sometimes though it is handy to test a command before using it as a subset of a bigger command.

Example: if I want to remove all sendmail RPMs from my machine, I can do so by running something like:

[leonid@home tmp]# rpm -e `rpm -qa | grep sendmail`

While I am sure this command works, I am not completely aware of which exactly RPMs it will remove. So, before I run it, I want to test:

[leonid@home tmp]# rpm -qa | grep sendmail
sendmail-cf-x.y.z
sendmail-x.y.z

Ok, now I am sure and I want to remove all those. One way would be to actually hit the Up arrow, put the backtick quotes around the whole thing, then add “rpm -e” in the beginning and we are done. All that movement is annoying to some (me, for instance). So, here is what I do instead:

[leonid@home tmp]# rpm -e `!!`

Yes, you are right, those are two exclamation marks. Easy, huh? :) Aparently, exclamation mark usage can do more good then jus that. For example “!se” will run the last command beginning with “se” in your history file. I’ve got used so much to this feature now that I cannot even imagine myself without it anymore. :)

P.S.: For purist and RPM fanatics: yes, I could have used the simple “rpm -e --test `rpm -qa | grep sendmail`” thingy, but I needed the example, so… :)

Leave a Comment