commandlinefu.com is the place to record those command-line gems that you return to again and again.
You can sign-in using OpenID credentials, or register a traditional username and password.
Subscribe to the feed for:
This gets the Nth argument in the last line of your history file. This is useful where history is being written after each command, and you want to use arguments from the previous command in the current command, such as when doing copies/moving directories etc.
I wrote this after getting irritated with having to continually type in long paths/arguments.
You could also use $_ if all you want is the last argument.
There are 2 alternatives - vote for the best!
Bash's history expansion character, "!", has many features, including "!:" for choosing a specific argument (or range of arguments) from the history. The gist is any number after !: is the number of the argument you want, with !:1 being the first argument and !:0 being the command. See the sample output for a few examples. For full details search for "^HISTORY EXPANSION" in the bash(1) man page.
Note that this version improves on the previous function in that it handles arguments that include whitespace correctly.
If you can do better, submit your command here.
You must be signed in to comment.