Configuring Bash

David Blackman

Issue #76, August 2000

A quick introduction to the Bash shell.

Welcome to the world of Bash, most widely used shell in Linux. Bash is surprisingly configurable, and, by the time you finish reading this article, you'll have an environment more comfortable for you. Bash does not differentiate between internal shell variables and external environment variables. A shell variable is a variable (usually all caps), associated with a value, and carried around between shells. Many programs use their own variables, like PILOTRATE, which they check. Bash has its own variables, like MAIL, that are important to it. Environment variables are set using the syntax:

export VAR=VALUE
or in two lines:
VAR=VALUE
export VAR
To check the value of an environment variable, type echo $VAR, or to see all set variables, type env. bash executes your ~/.bash_profile file for the login shell (on the console), and ~/.bashrc for non-login shells (xterms and the like). Often you may just want to link one to the other. If you export a variable, or set an alias on the command line, it only stays active for that one bash session. You must put it in your login script for it to stick. If you start having a monolithic .bashrc file and want better organization, you can split it up. Often, people break up their .bashrc into aliases, variables and functions, and the .bashrc simply executes the others. To have your .bashrc execute other files put in a line like this:
source FILE

The Prompt

The first environment variable we'll discuss is PS1. PS1 stores a character string that is interpreted by bash for use as your prompt. Here is a sample PS1 and its generated prompt:

PS1='<\u@\h:\w>$'
<blackmad@moomintroll:/etc>

Backslashed characters are interpreted, while other characters are displayed verbatim. \u is translated to user name, \h is translated to host name up to the first period, and \w is the working directory. Some of the most important backslashed characters, which can also be found in the bash manpage, in the PS1 section, are shown in the table “Interpreted Characters.”

Table 1. Interpreted Characters

Cool Xterm Title

One of the cool things in all X terminal emulators (xterm, rxvt, Eterm, ...) is that if you print "\033]0;STRING_HERE\007", the title of the term changes to STRING_HERE. Try it by typing:

echo -n "\033]0;Be Happy\007"

What I do with this is put a small function in my .bashrc function xtitle (see Listing 1), and I call this after I've set my PS1 variable, so at the end of my .bashrc file I have the line:

PS1=''\u@\h:\w>$''
xtitle
export PS1
This means that if I'm in a terminal emulator, it will set TITLEBAR, a string which will append user@hostname:directory to my prompt string (so it's printed each time I get a new prompt), and then export it. (Note that if your terminal emulator sets $TERM to something other then xterm* or rxvt*, you need to add another case, with | WEIRD_TERM_ENV on the line with xterm* | rxvt*) before the close parenthesis.

Listing 1. Function xtitle

Aliases

One of the most useful things to use with Bash is aliases. Aliases simply direct Bash to interpret a text string as something else. For example, you can fix it so that when you type happy, Bash interprets it as:

echo I'm a shiny happy shell

All aliases take the same form:

alias ALIAS="COMMAND"
Often, you may want to change the default behavior of a command, such as ls. I alias ls in this way:
alias ls="ls -aF --color"
ls now prints all files, in color, with classification. \ls will execute the unaliased command. Other times you may decide to define a whole new command in order to shorten the amount of repetitive typing. Here are a few aliases I use:
alias mkall=\
        "./configure && make && sudo make install"
alias whizz="ssh whizziwig@www.whizziwig.com"
alias tgz="tar -xvzf" alias ll="ls -aFl"
alias ls-d="ls -Sc"
These all save time and keystrokes, and since anything you type after the alias is still passed to Bash, it will just translate the part that is aliased. In my case, executing tgz linux-2.2.14.tar.gz actually executes tar -xvzf linux-2.2.14.tar.gz.

Mail

The $MAIL variable specifies which mailbox you want Bash to poll for new mail. You generally want to set this to your inbox. I use procmail, so I have many mail folders. My inbox (where mail that's actually addressed to me gets sent) is /home/blackmad/Mail/inbox, so when I get new e-mail there, Bash tells me: “You have mail in /home/blackmad/Mail/inbox.”

PATH

The PATH variable determines where, and in what order, Bash will look for executables. Each directory is separated by a colon (:). Bash interprets your path from right to left. Let's say your PATH is set to /usr/bin:/bin/:/sbin/:/sbin/. When you enter a command, Bash will look for it in its internal shell functions first, then /usr/bin, then /bin and so on, until it either finds the command or gets to the end of your PATH. Often you may simply want to append or prefix your current PATH; you can do this by specifying

PATH="$PATH:/next/path:/next_next/path"

or

PATH="/prev/path:$PATH:/next/path"
export PATH
In the first example, Bash will look through /next/path and /next_next/path after it finishes with your current PATH. In the second example, Bash will first look in /prev/path. You may want to prefix your PATH with /usr/local/bin, since that is where hand-compiled programs are usually located, and these are generally more recent then those that came with your distribution. You may also want to prefix ~/bin and have a bin directory in your home directory where you can put customized versions of programs and scripts (useful if you don't have root on the box).

The Last Step

Since Bash just runs through your .bashrc file and executes everything in it, you can toss in programs you want to run each time you log in. At the end of my .bashrc file, I have the following:

fortune
mesg y
users

So whenever I log in, fortune greets me with a bit of wisdom, messages are turned on, and I find out who's logged in to the systems.

David Blackman is a sophomore and a system administrator at Stuyvesant High School. He hopes to write the killer application for Linux soon and get hired by VA Linux Systems. He loves Perl, even though he knows it's evil, and enjoys the pointer arithmetic of C.