3 ms·
Wow, I did not know that! I would have referred to this using !$, which is one of the Bourne shell history substitutions. Like $ mkdir awesome $ cd !$ (T
by schoen 5y ago
Wow, I did not know that!
I would have referred to this using !$, which is one of the Bourne shell history substitutions. Like
$ mkdir awesome
$ cd !$
(The "!" has the connotation "history" in the shell, while the "$" has the connotation "last", as in regular expressions.)
An advantage of the way you mentioned is that you could actually see the history item displayed on the command line before running the command!
- senjin 5y agoI use !$ multiple times every day but you just helped me make the connection to try !^ for the first argument and I think I'm going to start using that as well. Thanks!
- schoen 5y agoThere is also "!!" for the entire prior command line. Someone elsewhere in this thread mentioned the usefulness of sudo !! for "run the entire previous command under sudo". Another example could be echo "!!" > myscript for "save the previous command into the file myscript".
- senjin 5y agoI also use cat `which !!` every once in a while
- aasasd 5y agoAt least in zsh, there are also !:1 for the first argument, et cetera with the rest of natural numbers, and something like !:* for all the arguments without the command. The generic form is !n:m for n-th last command line and m-th argument, with an omitted n being a shortcut for the last line. Not sure if all of this works the same in bash.
- senjin 5y agoI use zsh so this is also going in my toolbox. I think I've reached for !:* in the past and didn't know it so this is useful
- deleted 5y ago[deleted]
- gnubison 5y agoHistory expansion originated in csh, and was never in the Bourne shell. Bash, certainly.
- schoen 5y agoThanks for the correction. I often recognize when bashisms are bashisms, but I didn't realize that was.