6 ms·
The special bash command I'm most often asked about by shoulder surfers is !$. It substitutes the last argument in the previous command into the current one.
by crusso 14y ago
The special bash command I'm most often asked about by shoulder surfers is !$.
It substitutes the last argument in the previous command into the current one.
For example:
$ ls /some/long/path/somewhere/looking/around/
<output>
$ cd !$
cd /some/long/path/somewhere/looking/around/
- yan 14y agoI find ESC, . (esc, then press period) to be more intuitive. (alt+period works as well)
- kps 14y agoAn arguably better (and slightly more portable) way to accomplish this is the special variable $_ , which expands to the last argument of the previous command. Since $_ is a variable rather than a history substitution, it still works when there is no command history (e.g. in scripts) and allows all the usual variable expansion forms, for example ${_##*/} to extract the last path component.
- crusso 14y agoThat's good to know, although !$ is easier to type since you only need to depress the right shift key, whereas yours requires a quick shift on the opposite side. Makes a big difference when you're trying to quickly type "rm -rf !$" as root. :)
- garethadams 14y agoI don't think I'd type `rm -rf [anything]` quickly as root
- crusso 14y agoHence the smiley. Was a joke.
- seanp2k2 14y agoAs a righty, I rarely ever use the right shift key. Also, check it: $ cd /happily/tabbing/out/some/really/deep/path/ooh/dear/maybe/its/java/WAIT_A.file bash:> 'WAIT-A.file' is not a directory $ nano $_ (opens file)
- vilgax 14y agoBetter than that for me is "Alt+.", much easier to type. It can be also combined with number like "Alt+2+." will insert second argument. Similarly "Alt+0+." will insert last command. http://linuxcommando.blogspot.in/2009/05/more-on-inserting-arguments-from.html http://linuxcommando.blogspot.in/2009/05/more-on-inserting-a...
- micampe 14y agoIf you are on OS X and use Terminal.app “Alt-.” won’t work because Alt is used for alternate characters. You have two options: enable “use option as meta” in the app settings (but you lose the extra characters) or use “Esc-.” instead. Yes, I know about iTerm. I don’t want it.
- philsnow 14y agoIn my bash/readline/whatever, Alt+<N>+. gives the Nth-from-the-end argument to the previous command. so, $ echo a b c d a b c d $ echo # pressing <Alt+<2>+.> here inserts 'c', not 'b'.
- nicholassmith 14y agoI like that one and `!!` for the inevitable point I've forgotten to put sudo in before a command.
- nbouscal 14y agoI find myself typing `sudo !!` on a daily basis. You'd think I'd eventually learn to remember to type sudo the first time, but nope.
- LandoCalrissian 14y agoThat is really a good one, thanks.
- jevinskie 14y agoIs there one for all but the first argument?
- ampersandy 14y ago!!:2-$