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

My most used bash command is probably "sudo !!", which runs the previous command with "sudo" prepended.

The book "Unix Power Tools" showed me that higher-level programming languages (Perl, Python, Ruby, etc) are not needed to accomplish most administrative tasks. Awk is incredibly useful (but also very confusing, imo).



I really like '!$' and '!:1' in bash. '!$' holds the last argument of the last command and '!:n' holds the nth argument.

$ cd /big/long/path/to/this.txt Oops $ vim !$


It should be noted for onlookers that this is the abbreviated form; !-3:4 goes back to the third-to-last command and refers to its fourth argument, and similarly !-3:$ is its last argument.

It's also apparently not universally known that bash has csh-style (similar to sed) editing, so "s^foo^bar" will repeat the previous command with "bar" substituted at for bar. So "ls foo" becomes "ls bar".

Similarly with !-3:s^foo^bar

I freely mix these things with readline-editing, depending on what needs to be done.

Since csh gets a lot of disrespect these days, to give credit where due: these are all Bill Joy features from the original csh -- it's a good thing that bash borrowed innovations from various shells beyond Bourne shell and ksh.

Historical footnote: We donated code to him to do readline-style editing circa 1978, but he said raw mode was bogging down the pdp 11 too much. Systems were a bit slower back then.


If using readline (emacs) keybindings, press Alt and period for the same effect as !$.




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

Search: