Linux Babble 123-reg - Cheap domain names, free configuration

Shell variables

May 17, 2003 at 19:06, by Geoff Richards

A shell variable is a value that a Unix or Linux shell (such as Bash) keeps in memory. Each variable's value is a simple piece of text, and it has a name. Shell variables are sometimes used to configure programs, including the shell itself. They are also used in writing shell scripts.

Below we show how to use shell variables in general, and then give some concrete (and hopefully useful) examples of using them.

Displaying and setting shell variables

To see a list of the shell variables which are currently set, use the set command (although note that set will also sometimes show other shell settings):

$ set
BASH=/bin/bash
BASH_VERSION='2.05a.0(1)-release'
COLUMNS=80
CVSROOT=/var/cvs
HOSTNAME=blacktooth
PATH=/usr/local/bin:/usr/bin:/bin:/usr/bin/X11
...

To set a variable to a value, give it's name, an equals sign, and then the value you want to set it to (don't put any space on either side of the = symbol):

$ fruit=oranges

To print out the value of a particular variable, use the echo command. The echo command doesn't know anything about variables, but you can tell the shell to insert the variable's value onto the command line by putting a $ sign in front of it:

$ echo $fruit
oranges

The meaning of ‘environment variable’

The important thing to remember is that environment variables can affect any program you run from the shell, but shell variables can only affect the shell itself.

Some of the shell variables are passed to programs when you run them from the shell. These special variables are called environment variables. You can make a shell variable into an environment variable using the export command. This is known as exporting the variable:

$ export fruit

If you know in advance that the variable you are setting should be an environment variable, then the two previous commands can be combined:

$ export fruit=apples

To display a list of environment variables, use the env command. Remember that environment variables are also shell variables, so the set command will also show them, and they can be inserted into a command by prefixing their names with $.

The names of variables are case sensitive, so variables called foo and FOO are distinct.

Some useful variables

The single most important variable is called PATH. The shell uses it to find programs when you try to run them. PATH should contain a list of the directories in which program files reside, separated by colons. For example:

$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/bin/X11

If PATH has the value shown above, then the shell will first look for programs you run in /usr/local/bin, and then in /usr/bin, and so on, until it finds them. If you want to add the directory /home/fred/bin to this list, so that you can install some programs in your home directory, the following command would do the trick, using the existing value of PATH and adding the new directory at the end:

$ PATH=$PATH:/home/fred/bin
$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/home/fred/bin

The variable PS1 is also used by the shell. It can be used to change the prompt which Bash prints when it is waiting for you to type a command. This may not sound very useful, but the prompt can be made to contain useful information, like the hostname of the computer the shell is running on, or your current directory, helping to remind you of your context. The Bash prompt HOWTO has details.

One final example. The http_proxy variable is used by some programs to configure a web proxy. This one has to be an environment variable, so that these programs get a copy of it when you run them. Programs which use this technique for configuring an HTTP proxy include the text-only web browser Lynx and the command wget, which downloads files (or entire websites) from the web. You might set this variable like so:

$ export http_proxy=http://proxy.example.com:3128

This will tell programs that they should send HTTP requests to the proxy server on proxy.example.com, which is listening on port 3128 (the usual port for web proxies). Unfortunately, graphical browsers tend to ignore this variable, so you have to use their preferences dialogs to set the proxy.

Making variable settings permanent

The shell only keeps its variables in memory, so these settings get lost when the shell exits. To make your changes permanent, set the variables in one of the shell configuration files. These files just contain normal shell commands, so once you've typed the correct commands interactively and got them working, you can just copy the same commands into the appropriate file.

Exactly what is the ‘appropriate file’? That's a difficult question, because Bash has quite a complex way of deciding which configuration files to read when it starts up, and some Linux distributions have set things up in surprising ways. Usually the file .bash_profile in your home directory is the best bet. If that doesn't work, you might try .bashrc instead. These will both only affect your own user account—to make changes to variables affect all users on the system, set them in /etc/profile.

Related Links