![]() |
![]() |
Shell variablesMay 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 variablesTo see a list of the shell variables which are currently set, use the
$ 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 $ fruit=oranges To print out the value of a particular variable, use the $ echo $fruit oranges
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 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 The names of variables are case sensitive, so variables called
Some useful variablesThe single most important variable is called $ echo $PATH /usr/local/bin:/usr/bin:/bin:/usr/bin/X11 If $ PATH=$PATH:/home/fred/bin $ echo $PATH /usr/local/bin:/usr/bin:/bin:/usr/bin/X11:/home/fred/bin The variable One final example. The $ export http_proxy=http://proxy.example.com:3128 This will tell programs that they should send HTTP requests to the
proxy server on Making variable settings permanentThe 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 |
© Copyright Geoff Richards, 2004 |