Draconis Software Blog

Advanced Bash Features

Bash is probably the most popular shell, and is often the default shell in many Linux systems. In addition to features like tab completion, redirection and scripting functions which are popular, there are also many less commonly-known features which can be quite useful. Many of these advanced features are introduced in this guide (see also Part II).

There are two features in particular that I find particularly useful. First is the various history features in bash. Typing `history’ will give you the commands you’ve entered in the order they were entered. However there are many other ways to access the command history:

This brings us to bang-bang or !!. !! tells bash “repeat the last command I entered.” But the magic doesn’t stop there, if you order now, you’ll also receive !xyz. !xyz will allow you to run the last command beginning with xyz that you typed. Be sure to add enough to the abbreviation to make it unique or you could run into problems, for instance: In the example above, ps was ran twice, then pwd. If you typed !p you’d get the output of pwd. Typing !ps is just enough to be unique and will execute the ps -auxw | grep http entry in history. By typing just enough to make your history request unique, give you a much better chance of hitting your targeted command.

Another nice feature is the ability to move around and delete words using keyboard commands:

Once again, bash comes through for us. Here are some of the movement keystrokes that I use most often:

  • ^w erase word
  • ^u erase from here to beginning of the line (I use this ALL the time.)
  • ^a move the cursor to the beginning of the line
  • ^e move the cursor to the end of the line

There are more of course, but those are the ones you simply can’t live without. For those who don’t know the ^N notation means ctrl+N, don’t confuse it with hats mentioned above.

This guide also discusses some other nice tools like brace expansion, bash functions and aliases.

Additional Reading:

Share and Enjoy:
  • Digg
  • Reddit
  • Ma.gnolia
  • del.icio.us
  • BlogMemes
  • Facebook
  • Mixx
  • Furl
  • Google
  • TwitThis
  • Spurl
  • LinkedIn
  • Propeller
  • E-mail this story to a friend!