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.
Got Feedback? Join the discussion at discuss.howtogeek.com
Programmer by day, geek by night, The Geek, also known as Lowell Heddings, spends all his free time bringing you fresh geekery on a daily basis. You can follow him on Google+ if you'd like.
- Published 07/26/10



