Skip to Content »

Tech Life of Recht » archive for 'bash'

 Effective Bash

  • February 12th, 2008
  • 12:22 am

More and more people are coming into contact with Bash, primarily through Mac OSX and Ubuntu. All very nice, but most people really don't realize the full potential of the Bash shell, so here are some advices on how to use Bash effectively (note to more advanced users: yes, some of the features are due to other facilities such as readline, but that's not so interesting here). There's also a quite large topic on scripting which will not be discussed here. I recommend the Advanved Bash-Scripting Guide, and you should always be familiar with the manpage for Bash.

Configuration

Place all persistent changes in ~/.bashrc, which will be loaded every time a new shell is started. There's no need to restart the computer, just start a new shell (or type bash in an existing).

History

Use the up/down arrows to go through the recently executed commands. This is basic, these commands will make it even better:

CODE:
  1. export HISTCONTROL=ignoredups
  2. export HISTSIZE=10000
  3. export HISTFILESIZE=10000
  4.  
  5. shopt -s cmdhist

Setting these will result in two things: Repeated commands will only appear once in the history, and the number of commands in the history will be increased to 10000.

Better prompt

Especially when using ssh, it's easy to lose track of which machine you're on. Setting an appropriate prompt can help. My prompt looks something like this:

CODE:
  1. recht@challenger: /home/recht>

In other words, it contains my current username, hostname, and directory. Also, the prompt is configured to set the title of the terminal to the same:

CODE:
  1. export PS1="${TITLEBAR}\[\033[40;37;1m\]\u@\H: \[\033[37;40;0m\]\w> "

Aliases

Often used commands can be alised to a simple command. For example, the editor nano has an extremely annoying feature where lines are automatically wrapped unless nano is started with -w. An alias can make sure this is always so:

CODE:
  1. alias nano="nano -w"

Other useful aliases:

CODE:
  1. alias rm="rm -i"
  2. alias cp="cp -i"
  3. alias mv="mv -i"
  4. alias ls="ls --color=auto"
  5. alias du="du -h"
  6. alias more="less"

Auto completion

Autocompletion means entering a part of a command or filename, and then hitting TAB. If the command can be uniquely identified, the rest of the command is completed automatically, otherwise the possible completions are shown, and more characters can be entered.
By default, Bash only completes filenames, but it can actually do a lot more. This can either be hand coded, or you can use the completions which come with Bash.
The completions file is normally located in /etc/bash_completion. Use it by entering

CODE:
  1. source /etc/bash_completion

After this, almost anything can be completed: ant tasks, make targets, ssh hosts, and much more.Take a look in /etc/bash_completion for the complete list.

One other practical thing to set is this in ~/.inputrc:

CODE:
  1. set show-all-if-ambiguous on

If no unique completion is available, the possible completions are shown automatically.

Keyboard

It's possible to use the keyboard to navigate commands, history and just about anything else.
The available keyboard commands can be seen in the manpage for Bash, but here are some of the most important:

  • Ctrl-a: Go to beginning of line
  • Ctrl-e: Go to end of line
  • Alt/Meta-b: Go back one word
  • Alt/Meta-f: Go forward one word
  • Ctrl-r: Search history backwards. Press Ctrl-r and enter part of an earlier command to find it in the history. Press Ctrl-r to search again.
  • Ctrl-d: Delete character
  • Ctrl-d on an empty line: Exit the shell
  • Ctrl-s: Stop sending data to the terminal. Nothing will be changed before pressing Ctrl-q
  • Ctrl-q: Start sending data to the terminal

Beeping

Just about the most annoying thing any computer can do to me is beep or make any kind of unexpected sound - which is just about any sound not coming out of an MP3 player. The shell beeping is among the worst. Trade it with a flash by putting this in ~/.inputrc:

CODE:
  1. set bell-style visible

Changing directories

The cd command is use to change the current directory. Simple, but there's still room for improvement.

If you mistype a directory, something which actually happens pretty often when you use completion without a unique name and hit enter without choosing the correct dir, an error occurs. However, Bash can also be configured to take a best guess:

CODE:
  1. set -s cdspell

For example, if I have to directories doc and download and type "cd do", Bash will select doc for me. Of course, the correct dir is not always selected, so this option might annoy some more than others.

Typing cd with any arguments will return you to your home directory. Typing "cd -" will return you to the previous directory - nice when executing commands in two different directories.

Pager (less)

The pager is used when displaying text files. In the good old times, this was more, but the GNU alternative is less. There's usually no reason to do it, but just to be safe, do

CODE:
  1. export PAGER=less
  2. [code]
  3.  
  4. The standard configuration of less can be improved, however. My configuration looks like this:
  5. [code]
  6. export LESS='-i  -e -M -X -F -R -P%t?f%f \
  7. :stdin .?pb%pb\% :? lbLine %lb:?bbByte %bb:-...'

This basically enables case insensitive search, quit when end of file is reached, quits at once if entire content fits on the screen, and gives a nice prompt with line numbers. Look in the manpage for less for more info.

Just a few keyboard commands for less: Hit / and enter a string to search. Press n or N to search again. Enter a number and press g to go the the corresponding line number. Press q to quit.

Job control

Commands can be executed in a shell by typing in the command name and arguments. If the last character on the line is a '&', the command will be run in the background. However, when the shell is closed, all background processes are also closed, so don't rely on '&' to run your server processes.

When processes are running in the background, type 'jobs' to see the processes and their status. If you want any of them attached again, run 'fg', possibly with the job number.

An attached process can be backgrounded by pressing Ctrl-z. This will stop the process and release the terminal. If you want the program to continue running while you're doing something else, type 'bg'.

This concludes what I see as the essential features of Bash (and readline and a couple of other things). If you do use Bash on a daily basis, do yourself a favor and continue with the Bash manual to discover more keyboard shortcuts. It might also be interesting to check up on the features of your terminal. I personally use urxvt, which can run as a daemon to avoid unnecessary overhead, plus it has some other nice features such as resizing.