Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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/


An 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.


That'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. :)


I don't think I'd type `rm -rf [anything]` quickly as root


Hence the smiley. Was a joke.


As 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)


Better 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-a...


If 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.


iTerm2 by chance? Carries a great deal of improvements over the original iTerm and is under active development.


One way or another, that's true of any terminal. The shell sees characters, not keystrokes.


In 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'.


I find ESC, . (esc, then press period) to be more intuitive. (alt+period works as well)


I like that one and `!!` for the inevitable point I've forgotten to put sudo in before a command.


I 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.


Is there one for all but the first argument?


!!:2-$


That is really a good one, thanks.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: