Sunday, March 15, 2009

Tip #3: Referencing the last argument to the last command in Bash

This command is no secret to intermediate and advanced Bash users, but it is one that all users should know since it comes in handy all the time.

It is often useful to reference the last argument to the most recently executed command in Bash. For instance, if one were to copy a file to a given directory, and then want to copy another file to the same directory, one might not want to type the entire path to the directory again. Fortunately, with the !$ operator, this is unnecessary.

linux@devbox:~/tip3> cp test1.png /root/Desktop/images/tip3
linux@devbox:~/tip3> cp test2.png !$


In the example above, instead of having to retype /root/Desktop/images/tip3 when performing the second copy, the !$ operator was used instead, which is replaced with the last argument of the last command executed.

No comments: