• ARTICLES
SEARCH

How-To Geek

Save the Last Executed Command to a File

Have you ever typed out a really long command, realized it worked great, and then wanted to save it to a file? There’s an easy bash trick you can use to do just that.

echo “!!” > filename

I sometimes use this trick when I’m building a script, but I’m not sure exactly what commands I want… so I type them out on the shell to make sure they execute properly, and then use the same command but with >> to append them to the end of the script I’m working on, like this:

echo “!!” >> scriptname.sh

If you execute another command after the last one, you can save that one like this:

echo “!-2″ >> scriptname.sh

Very useful, or at least fun to learn.

Lowell Heddings, better known online as the How-To Geek, spends all his free time bringing you fresh geekery on a daily basis. You can follow him on if you'd like.

  • Published 07/26/10

Get Free Articles in Your Inbox!

Join 134,000 newsletter readers

Email:

Go check your email!