A brief introduction to the bash shell
particularly for folks who use tcsh or csh
and would like to give bash a try!

Configuration Files

~/.bash_profile
The contents of this file are read and executed when you log into the system. Any changes you make to this file will not come into effect until you log out and log in again. It is usually a good idea to have this file execute your ~/.bashrc file as well. Just add the line "source ~/.bashrc" to the end of this file.. Beware that if you define your aliases here they will not be available in your subshells.
~/.bashrc
The contents of this file are read and executed only when you start a new subshell. Add your aliases here.
~/.bash_logout
This is executed when you log out. You may want to add some commands here to remove any temporary files or do any other tidying up.

Environment variables

In general variables are set as follows:

foo=bar
You can check the value of the variable with:

echo $foo
For a variable to be an environment variable it must be exported. You can define it and then export it like this:

foo=bar
boo=woo
export foo boo
Or you can save yourself some typing by doing it like this:

export foo=bar
export boo=woo
Summary of bash variables

File name completion

File name completion is almost identical with t/csh, with the following exception. If there are multiple possible completions to a file name that you are attempting to complete bash will beep. The possible completions can be listed by hitting the "tab" key again. (In t/csh you would use ^D)

You can also have certain types of file excluded from the file name completion mechanism. Set the FIGNORE environment variable to a colon separated list of the file name suffixes you want ignored. See the example ~/.bash_profile below.


Aliases

The "alias" command creates and lists aliases. Use it by itself to list your aliases, or like the following to create new aliases

alias ll='ls -lF'
To remove the previously defined alias, use...

unalias ll
Warning: unlike csh, bash aliases do not accept parameters. If you have aliases that use parameters you can change them to simple functions. For and example have a look in the annotated .bashrc below, or just read the next section. :-)


Functions

If you're a t/csh user and have grown accustomed to the convenience of using aliases with parameters, don't despair. Although bash doesn't let you use variables with aliases you can easily write functions with parameters in them.

Defining simple functions

Here is an example of a simple function taking one parameter.

cd() {
	builtin cd $1
	pwd
}
This version replaces the builtin cd with one that prints out the current directory after the directory is changed. You can define any number of functions either directly at the command line for temporary ones, or add them to your ~/.bashrc file.

Parameters

The positional parameters hold the command line arguments to the function. $0 is the name of the script or function that was invoked at the command line. $1, $2, $3, ... are the respective command line arguments.

For more information see "Learning the bash shell", page 87.


Changing over to bash

If you want to change over to bash as your default shell, you need to take the steps listed below. A well set-up bash environment feels very similar to the tcsh.

  1. Create the files ~/.bash_profile and ~/.bashrc using the samples below. Then edit them to include your own environment variable preferences and aliases. Note that the ~/.bash_profile file below reads the value $PATH from a file ~/.path which contains a list of directories separated by newline characters (with no other whitespace). You should make sure that you keep a record of your current path - otherwise you may not be able to access the programs that you usually use.
  2. Put the filename of your mailbox into a file called ~/.mailpath
  3. Ensure that you have changed your aliases and variable settings in the above files to bash style.
  4. Run "chsh" to set your default shell to bash.
  5. Log in and see how you like it.

A sample annotated ~/.bash_profile

This file is executed when you first login, the variables are all exported so that they are available to all subshells.

This has a bit of tricky programming in it where all of the paths are stored in separate files - so that they can be easily changed without modifying the original ~/.bash_profile file.

# ~/.bash_profile
# Executed by login shells

# Converts a \n separated list into a colon separated list
colonise() {
	/bin/cat $1 | /bin/tr "\012" ":"
}

### Variables used by bash itself

# Paths...
# these all use the above "colonise" function to
# convert a list of paths on separate lines into
# a colon separated list.
export PATH=`colonise ~/.path`
export MANPATH=`colonise ~/.manpath`
export MAILPATH=`colonise ~/.mailpath`
export CDPATH=`colonise ~/.cdpath`

# Control history
# I don't like to have a lot of 
# old commands hanging around.
export HISTFILESIZE=10
export HISTSIZE=10
export HISTCONTROL=ignoreboth

# Control file name completion: ignore the following suffixes
export FIGNORE=`colonise ~/.fignore`

# Exiting bash deliberately and involuntarily
# export IGNOREEOF=1
export TMOUT=3600

# Prompt
export PS1="[\u@\h] \W [\!] "

### Variables that don't relate to bash

# Set variables for a warm fuzzy environment
export CVSROOT=~/.cvsroot
export PGPPATH=~/.pgp
export EDITOR=/usr/local/bin/emacs
export PAGER=/usr/local/bin/less
export PRINTER=134

# Execute the subshell script
source ~/.bashrc

# end of ~/.bash_profile


A sample ~/.bashrc which is executed each time a shell or subshell is started. I just contains aliases, some functions and a couple of miscellaneous settings.

# ~/.bashrc 
# executed by login and subshells

# aliases

# search man pages by subject
alias a='man -k'

# edit and re-read this file
alias be='$EDITOR ~/.bashrc ; source ~/.bashrc'

# add the current directory to the "cdpath"
alias cdpath='pwd >> ~/.cdpath ; export CDPATH=`colonise ~/.cdpath`'

# abbreviations for some common commands
alias f=finger
alias h=history
alias j=jobs
alias l='ls -lF'
alias la='ls -alF'
alias lo=logout
alias ls='ls -F'
alias m='less -a -i -P='
alias weather='telnet vicbeta.vic.bom.gov.au 55555'
alias which='type -all'
alias xman='xman -bothshown -notopbox &'

# functions

# A new version of "cd" which
# prints the directory after cd'ing
cd() {
	builtin cd $1
	pwd
}


# add the directoried specified to the path file
# and re-read it.
pathadd() {
	for p in $*
	do
		echo $p >> ~/.path
	done
	export PATH=`colonise ~/.path`
}


# Other settings
umask 027
mesg y

# end of ~/.bashrc 
# Uncomment the following if you prefer vi style command editing 
# to emacs style

# set +o emacs
# set -o vi

# Important startup procedures...
/usr/local/games/bin/fortune
echo

# end of ~/.bashrc
You can also create a ~/.bash_logout file that is executed whenever you log out.


References


Ted Smithwebmaster@physics.ucsb.edu Oct 1996