5 Simple Bash Tips, Part III

Posted by Chris7mas on Aug 12, 2009 3:26 PM EDT
Tux Arena; By Craciun Dan
Mail this story
Print this story

This is the third article in the Bash tips series, you can find the other two here and here. 1. Use cd - to go to the previous working directory

This is the third article in the Bash tips series, you can find the other two here and here.

1. Use cd - to go to the previous working directory
cd - is the same as cd $OLDPWD, where the $OLDPWD variable holds the previous working directory. You can also alias this to something like:

alias back='cd -' # or alias back='cd $OLDPWD'

If you've just opened a new Bash session or sourced .bashrc, the $OLDPWD variable will be unset.

2. Make copies of files easier
This one is especially useful for creating backup files, but not only. To fasten up rename commands like mv file1 file1bak you can use something like:

mv file1{,bak}

Here, Bash will expand this command into mv file1 file1bak and execute it afterwards.

3. Search history backward or forward
This one is very popular due to its usefulness. Add the following two lines inside your .bashrc file:

bind '"e[A"':history-search-backward bind '"e[B"':history-search-forward

Now type a command, for example ls, and then press the Up or Down arrows. You will see how it autocompletes the last ls commands. This will not change the default behaviour of Ctrl+P and Ctrl+N.

4. Use grep -e "-pattern" to search man pages for entries starting with -
This one is not necessarily a Bash tip, but you can use it to search man pages for entries that start with characters like -

man gcc | grep -e "-Wall"

5. Change the Bash prompt colour
You can customise your Bash prompt the way you like, including colours and what to display. Here's a simple example, just add this line at the end of .bashrc:

PS1="[?33[1;33m]$USER@$HOSTNAME$ [?33[0m]"

This will offer a nice yellow prompt like in the screenshot below:

Full Story

  Nav
» Read more about: Story Type: Tutorial

« Return to the newswire homepage

This topic does not have any threads posted yet!

You cannot post until you login.