skip to content
 

Customizing the bash shell and its startup files

Default Bash Shell Startup Files

In your home directory you will find files called

  • .bashrc
  • .bash_profile

Typing ls fails to see them because they are known as "dot files" or "hidden files". To see them use the -a switch with ls i.e. ls -a

These files are important and should never be deleted no matter how desperate you are for extra disk space (they are short text files so deleting them wouldn't help anyway). In the .bashrc and .bash_profile are commands that are executed when you log in. Any content you add to these files must be added after the line

# Add personal customisation commands after this line ------------------

Otherwise your changes may be overwritten when the central config files are run.

Should you accidentally muddle these files up or edit them in such a way that some common commands like matlab or pine no longer work for you, you can copy the default ones back into your home directory. To do this, type /alt/bin/shellreset then log out and back in to get rid of any broken sessions. This will take a backup of your startup files in case you have any customisations you want to keep.

Distinction Between .bash_profile and .bashrc

If you wonder why there are two separate files .bash_profile and .bashrc, it is because Unix distinguishes between login shells and interactive non-login shells.

  • When you ssh or putty in, it is always a login shell.
  • When you pop up a terminal window, it should be a login shell but for some old setups it may not be.
  • bash users can check whether it's a login shell with the command shopt login_shell
  • Login shells run .bash_profile on startup.
  • Non-login shells run .bashrc on startup.

It's best to put your customisations in .bashrc and have .bash_profile run .bashrc. Then your shell will behave the same whether it is a login shell or not. In the default setup, .bash_profile runs .bashrc via a central script. If you want to test whether this happens for you, add an echo command to .bashrc and run an ssh session (to ensure you get a login shell). Delete the echo command afterwards - read on to find out why.

There is one exception to the above rule: commands producing output should go in .bash_profile. The reason is that WinSCP runs a non-login shell and gets confused by output from .bashrc.

Example Edits / Customisations

  • Remember, all commands mentioned below should be added to your .bashrc to have a lasting effect, not just typed into a terminal window.
  • Your changes need to be added after any commands to run global configuration files or they may be overwritten. With the default Maths config files this means adding them after the lines
    #
    # Add personal customisation commands after this line ------------------
    #
    

Aliases

The alias command is used to rename a command to another or create a short alias for a long command. Some examples:

alias copy=cp

if you find it difficult to remember that the unix command cp is used to copy files.

alias mroe=more

to correct frequently mistyped words.

alias programb=/alt/applic/user-maint/abc99/bin/programb.pl

An alias to run a program installed in the shared software area. Alternatively consider adding /alt/applic/user-maint/abc99/bin to your PATH - see below.

alias embroil='cd /data/embroil/abc99'

An alias to change directory to where your data space is. (replacing user abc99 with your login and embroil with whatever computer you are using for data space). Then from any computer you can type the name of the alias - in this case embroil - and you will be put into your data directory there.

Environment variables

The bash shell has many environment variables (with names in CAPITALS) which affect its behaviour. You can see a complete list of them by typing set, or check the value of one variable with the echo command e.g. echo $HOME will tell you where your home directory is.

Note: modifying environment variables incorrectly can confuse your shell. If this happens, terminate that session and start a new shell without your changes.

The environment variables which you are most likely to need to change are PATH, PRINTER, VISUAL/EDITOR and PS1.

Note: Historically DAMTP users have set environment variables in their .env_extra file. If you have this file and your .bashrc calls it, take care not to set the same environment variable differently in both files.

PATH

PATH is the list of directories in which the shell searches for the commands you type. The default PATH is given by this line in our central Bashrc file:

export PATH="${HOME}/bin:/alt/bin:/usr/local/bin:/bin:/usr/bin:."

For your shell to work properly, your PATH must contain the directories

  • /alt/bin
  • /usr/local/bin
  • /bin
  • /usr/bin

and /alt/bin must come before the others. If not, you may find that you are missing some local commands or running an outdated version of LaTeX.

If your PATH is broken, consider running /alt/bin/shellreset to replace your startup files by the Maths defaults.

You can add more directories to your PATH, for example if I want the system to know about the programs I have installed in data space on say embroil, in a directory called my-programs, I might add that directory to my PATH, so when I type in the name of the program the system can run it without needing to be told every time where it is.

export PATH=$PATH:/data/embroil/abc99/my-programs/

Then if the system cannot find a program in any of the standard directories on my path it will check the above data directory. If some of the programs in the data directory have the same names as system-provided ones and I want my ones to take precedence, then I need to list the data directory first e.g.

export PATH=/data/embroil/abc99/my-programs:$PATH

PRINTER, VISUAL, EDITOR

The PRINTER environment variable is the name of your default printer. To set it, add a line to your .bashrc

export PRINTER=printer-name

and replace printer-name with the printer you would like to use as your default printer.

Programs such as the file viewer less and the email client mutt allow the user to edit files as part of their functionality. To do this they pop up a separate text editor, and they use the EDITOR variable (or sometimes VISUAL) to determine which editor to use.

In the default setup these variables are set to emacs. If you prefer vi then these are the lines to add:

export VISUAL=vi
export EDITOR=vi

PS1 (shell prompt)

In Maths the standard shell prompt takes the form

user@hostname:current-directory $

e.g. erm1001@solstice:~/tmp $. We use an alias for the cd command so that very long directory paths are trimmed. You can see the technical details using the type command at the command line:

type cd
type damtppromptcd
type damtppromptcm

Some people prefer to customise their prompt, and to do this you must disable the alias by including the following in your .bashrc:

unalias cd >/dev/null 2>/dev/null