May 272013
 

Bash is the de facto shell these days, and for good reason. Most of us who peck away at our shells all day become pretty good with Bash’s keyboard shortcuts, but there’s one handy and little known shortcut that’s worth mentioning – how to recall the first word from the previous line only.

You’re probably used to pulling up the previous Bash line with the “Up” arrow. You probably also frequently substitute the last word from the previous line with “Ctrl-Underscore”. But next time you need to execute the previous command, but with a different argument, rather than hitting “Up”, deleting the last argument and retyping the new argument, try using !:- in place of the first word (that is, the command).

So if the previous command was:

   # /opt/splunk/bin/splunk start

And you next wanted to execute it again, but with a “stop” argument, do this:

   # !:- stop

The Bash shell will substitute !:- with the text from the previous command. No grabbing for the mouse and copying and pasting. Remember, according to Larry Wall, creator of Perl, Laziness is the second virtue of the great programmer.


Matt Parsons is a freelance Linux specialist who has designed, built and supported Unix and Linux systems in the finance, telecommunications and media industries.

He lives and works in London.

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>