Personal tools
You are here: Home / cmgui / Wiki / Setting environment variables in bash
Navigation
Log in


Forgot your password?
 

Setting environment variables in bash

You can manually set an environmental variable in a shell by using a command of the form:

export VAR=value

For example:

export CMISS_ROOT=/home/pbie003/cmiss

This variable will only exist for this instance of your shell though. If you want every shell you start to have some environmental variables set you need to add an export command to one of the script files which gets sourced once you open up a new shell. There are a number of different configuration files which may already exist on your system (or can be created).

Different files are run depending on what files exist on your system and whether you are starting up a login shell or a non-iteractive shell (ie whether you have to login or not). To ensure that your environment is set up in either case I recommend adding commands to both ~/.bash_profile and ~/.bashrc

If the ~/.bash_profile file exists it will always be read when you start up a login shell. If the ~/.bashrc file exists it will always be read when you start up a non-interactive shell. If you do not add a line to the .bashrc file then you will need to log out and log back in to set up your environment (or start a shell with the --login option).

Here is some more detail on what is actually going on, taken from the bash manpage:

When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/profile, if that file exists. After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.

When an interactive shell that is not a login shell is started, bash reads and executes commands from /etc/bash.bashrc and ~/.bashrc, if these files exist. This may be inhibited by using the --norc option. The --rcfile file option will force bash to read and execute commands from file instead of /etc/bash.bashrc and ~/.bashrc.