System and enviromental variables
Friday, 5 October 2007, adz
System and enviromental variables define parts of the system behavior so it’s worth knowing what they are, what they influence and how to adjust them to your needs. This is the last part of the “console basics” series.
Wikipedia says that environmental variables are a collection of dynamic values having impact on the ways the active processes (programs) are run. System variables exist in every operating system — be it Unix, Unix-like, or MS-DOS, and Windows. Of course, different systems will enjoy different sets of those variables but most of the variables are shared by all the systems. Quite often new users are afraid to make use of the variables fearing that they could cause damage to their systems and treat the variables as out of reach items. This approach is wrong. Bash allows to us make shell variables which will be treated on the same principles as system variables but limited to the shell in which they were created.
1. Some Environment Variables
| Variable | Description |
|---|---|
| PATH | This variable comprises of a list of paths delimited by colons. This is the place the shell searches for a program which the user entered for execution. If the search fails the shell will display the message “command not found”. |
| EDITOR | Default editor. This variable is used by some programs e.g. “mutt” (console-based e-mail client). In the case of “mutt” all posts will be edited in editor declared by the EDITOR variable. |
| SHELL | A shell used by a user. |
| USER | User’s name. |
| SHLVL | Number of active shells. |
| TERM | Default terminal emulator. |
| HOME | Default path to user’s home directory. |
| UID | Unique user’s ID. |
| $LANG, $LC_ALL | Variables keeping local language settings (locale). |
2. Disclosing Variables
To do that we should use known command - echo $variable.
adam@laptop:~$ echo $USER $UID $SHELL $HOME
adam 1000 /bin/bash /home/adam
To display all variables we should invoke env command. The listing below shows only a part of full output. The same result can be obtained using set command which is built into the Bash shell.
adam@laptop:~$ env
SSH_AGENT_PID=5605
TERM=xterm
DESKTOP_STARTUP_ID=
SHELL=/bin/bash
GTK_RC_FILES=/etc/gtk/gtkrc:/home/adam/.gtkrc-1.2-gnome2
WINDOWID=58896938
GTK_MODULES=gail:atk-bridge
USER=adam
USERNAME=adam
DESKTOP_SESSION=gnome
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
PWD=/home/adam
LANG=pl_PL.UTF-8
GDMSESSION=gnome
HOME=/home/adam
SHLVL=2
LOGNAME=adam
3. Creating New Variables
A new variable can be made giving its name and a value, e.g. variable=value. Variable names use both capital and small letters so VARIABLE and variable declares two different variables. It is a common rule that the variables are written with capital letters.
adam@laptop:~$ VARIABLE=value
adam@laptop:~$ echo $VARIABLE
value
The variable we created above is limited to the shell in which it was called into existence. Programs run from other shells where the variable was not created will not be able to make use of it. To sidestep the snag we will make the variable available globally using the export command.
adam@laptop:~$ VARIABLE1=value1
adam@laptop:~$ VARIABLE2=value2
adam@laptop:~$ export $VARIABLE2
adam@laptop:~$ export VARIABLE2
adam@laptop:~$ bash #starting new shell
adam@laptop:~$ echo $VARIABLE1 $VARIABLE2
value2
Only one variable was exported in the above example. Its value was printed out in the new shell environment.
3. Storing Variable Values
All variables, both those local to the current shell and exported ones, will be available as long as the user is logged in. To have the variables ready to use in future sessions they have to be placed in the .profile hidden file. If they have to be available to every user of the system definitions of the variables must be added to the global configuration file /etc/profile.
Here is an exemplary line from .profile which will illustrate the text (it contains configuration data for an anonymous CVS server which enables downloading NetBSD source files).
export CVSROOT=anoncvs@anoncvs.netbsd.org:/cvsroot
4. Removing Variables
To have a variable removed from the current shell, the unset command should be issued.
adam@laptop:~$ VARIABLE=value
adam@laptop:~$ echo $VARIABLE
value
adam@laptop:~$ unset VARIABLE
adam@laptop:~$ echo $VARIABLE
This article is the last one of the Command line tricks series. Go back to the previous article: Regular expressions and search patterns »
Subscribe to RSS feed for this article!
2 Comments
- A hyperlink: <a href="polishlinux.org">GNU/Linux for everyone!</a>,
- Strong text: <strong>Strong text</strong>,
- Italic text: <em>italic text</em>,
- Strike: <strike>
strike</strike>, - Code: <code>
printf("hello world");</code>, - Block quote: <blockquote>Block quote</blockquote>













Not just subshells. Variables created in bash that are not exported are not added to the environment at all and, while their values might be substituted into a command line before it is run, they are otherwise invisible to called programs.
Also see:
printenvor I believe in some shells other than BASH
env