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. By convention, environment variables have UPPER CASE and shell variables have lower case names.
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 the setenv command, displayed using the printenv or env commands, and unset using the unsetenv command.
To show all values of these variables, type
% printenv | less
One of the major uses of environment variables is to provide information for
your programs when they are running.
You can interrogate the value of an environment variable from within
a C program using the getenv() function, which is accessible if you have
included stdlib.h in your program. Here is an example program
(modified from code by WB Langodon) that reads the PATH environment variable
and prints it out.
#include <stdio.h>
#include <stdlib.h>
int main(){
printf("test\n");
const char* s = getenv("PATH");
if(s == NULL)
printf("getenv returned NULL");
else
printf("PATH : %s\n", s);
printf("end test\n");
}
Enter this code using an editor, compile it, and run it.
The output should look something like this:
test
PATH:/usr/lib64/qt-3.3/bin:/usr/kerberos/bin:/opt/ucl/bin:/usr/sbin:/sbin:/usr/bin:
end test
An example of a shell variable is the home variable. The value of this is the home directory path. Type
% echo $HOME
SHELL variables are set by = command.
To see and change the length of history to store, type
% echo $HISTSIZE
% HISTSIZE=5
To show all values of these variables, type
% set | less
Each time you login to a UNIX host, the system looks in your home directory for initialization files. Information in these files is used to set up your working environment. The Bash shell uses a file called .bashrc (note that the file name begins with a dot).
At login the Bash shell first 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.
Use history as an example again, to change the number of shell commands saved in the history list, you need to set the shell variable history. It is set to 100 by default, but you can increase this if you wish.
% HISTSIZE=200
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, it will only have the default history value set. To PERMANENTLY set the value of history, you will need to add the set command to the .bashrc file.
First open the .bashrc file in a text editor. An easy, user-friendly editor to use is nano.
% nano ~/.bashrc
Add the following line AFTER the list of other commands.
HISTSIZE=200
Save the file and force the shell to reread its .bashrc file buy using the shell source command.
% source .bashrc
Check this has worked by typing
% echo $HISTSIZE
When you type a command, your path (or PATH) variable defines in which directories the shell will look to find the command you typed. If the system returns a message saying "command: Command not found", this indicates that either the command doesn't exist at all on the system or it is simply not in your path.
Type
% echo $PATH
This shows you what directories the shell will look in when a command is entered. Examining this will show you why, when you want to run a program from your current directory, you have to type something like
% ./myprogram
By default, the PATH variable does not include your current directory (i.e. ./), so the shell will not look there for myprogram unless you explicitly tell it to. Fortunately, it is easy to solve this problem, by simply adding your current directory to the beginning of your existing path ($PATH represents this) by issuing the commands:
% PATH=".":$PATH
% echo $PATH
The current directory "." should now appear at the beginning of your path. Now, the shell will look first in your current directory to find the command you have typed in, before looking in the other places that it would normally look. Test that this worked by moving to a directory where you have one of your programs compiled. Now, try running it by simply typing the program's name, without the leading ./
To add this update to your path PERMANENTLY, add the following line to your .bashrc AFTER the list of other commands.
PATH=".":$PATH
M.Stonebank@surrey.ac.uk October 2001