How do I modify my shell environment?
There are a number of customizations you can add to your shell environment, including aliases for common commands, changing your shell prompt, etc.
This document is currently specific to the bash shell. We may add more information about the tcsh shell later.
What kind of changes can I make?
You can pretty much do anything you want as a part of your shell. For example, some common operations include:
- Changing the look of your shell prompt
- Creating aliases to common commands
- Modifying environment variables, including:
- PATH, which specifies the list of paths that the shell looks for executable software
- LD_LIBRARY_PATH, which specifies where an executable can look for a run-time library
Where do I put the changes to make them persistent?
If you want to test out a change in an existing shell, just type the commands at the prompt, and see what happens. Then if it doesn't work the way you want it, just close the shell, and log in again, and your changes won't stick. Once you have it figured out, though, you probably want to put it in a file, so you don't have to type it every time.
By default, bash looks at a few files when the shell is created. Which one depends on what kind of shell you're creating, but we'll get to that in a minute.
The files are located in your home directory, and are named one of the following:
.bash_profile
.bashrc
Why two files? Which one should I use?
Bash Uses two separate files for different kind of shells. These are divided into login and non-login shells.
Login shells occur any time you have to authenticate (put in username and password), and you are presented with a shell prompt. So, if you log into our system via SSH, you'll end up with a login shell being created. Login shells read from the .bash_profile file
Non-login shells occur any time the shell is used for other purposes, including when you use something like screen to instantiate shells (you're not providing credentials), or you use SSH to execute a remote command without a full interactive shell, eg like this:
ssh username@hostname somecommand
In addition, non-login shells occur any time you run a script that uses bash. Non-login shells read from the .bashrc file.
In general, if you want to make changes that have relevance to your interactive shell sessions, put them in .bash_profile. If you want the changes to run every time you run a shell script, etc., then put them in .bashrc. If you want them to do both, then you can do one of the following:
- Put the changes in both scripts
- Have the .bash_profile script read from and use the .bashrc file, using code like this:
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
Example
This example shows a few common operations that you might want to do as a part of your bash shell:
.bash_profile:
# include my custom directory for finding programs to execute:
export PATH=$HOME/myprograms/bin:$PATH
# include my custom directory for finding link-time or run-time libraries:
export LD_LIBRARY_PATH=$HOME/myprograms/lib:$LD_LIBRARY_PATH
# modify my shell prompt to show the username, hostname, and current directory name:
export PS1='[\u@\h \W]\$ '
# make an alias for "py26" to point to "python26", to decrease what I have to type every time
alias py26="python26"
Last changed on Thu Jun 1 18:45:05 2017