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.

This article was written on 07/26/10 Tagged with: Linux

Daily Email Updates

You can get our IT articles in your inbox each day for free. Just enter your name and email below:


Name:
Email:

Leave a Reply

Our Network
About Sysadmin Geek