Variables are a way of passing information from the shell to programs when you run them. Programs look "in the environment" for particular variables and if they are found will use the values stored. Some are set by the system, others by you, yet others by the shell, or any program that loads another program.
Standard UNIX variables are split into two categories, environment variables and shell variables. In broad terms, shell variables apply only to the current instance of the shell and are used to set short-term working conditions; environment variables have a farther reaching significance, and those set at login are valid for the duration of the session.
An example of an environment variable is the OSTYPE variable. The value of this is the current operating system you are using. Type
% echo $OSTYPE
More examples of environment variables are
ENVIRONMENT variables are set using an = assignment and the export command, displayed using the printenv or env commands, and unset using the unset command.
To show all values of these variables, type
% printenv | less
An example of a shell variable is the HISTSIZE variable. The value of this is how many shell commands to save, allow the user to scroll back through all the commands they have previously entered. Type
% echo $HISTSIZE
More examples of shell variables are
SHELL variables are both set using an = assignment. They can be unset by using the unset command.
To show all values of all shell and environment variables, type
% set | less
The PATH environment variable specifies directories to search for commands and programs.
Each time you login to a UNIX host, the system looks in your home directory for initialisation files. Information in these files is used to set up your working environment. The bash shell uses many files, including one called .bashrc (note that all these file names begin with a dot).
When the shell starts, it reads .bashrc
.bashrc is used to set conditions and perform actions specific to the shell and to each invocation of it.
WARNING: NEVER put commands that run graphical displays (e.g. a web browser) in your .bashrc file.
For example, to change the number of shell commands saved in the history list, you need to set the shell variable history. It is set to 500 by default, but you can increase this if you wish.
% HISTSIZE=1000
Check this has worked by typing
% echo $HISTSIZE
However, this has only set the variable for the lifetime of the current shell. If you open a new xterm window (or login again), it will only have the default history value set. To PERMANENTLY set the value of history, you will need to add the variable set command to the .bashrc file.
First open the .bashrc file in a text editor. An easy, user-friendly editor to use is pico.
% pico ~/.bashrc
Add the following line AFTER the list of other commands.
HISTSIZE=1000
Save the file and force the shell to reread its .bashrc file buy using the shell source command (.).
% . .bashrc
Check this has worked by typing
% echo $HISTSIZE
M.Stonebank@surrey.ac.uk October 2001