Guide to Unix/Environment Variables
From Wikibooks, the open-content textbooks collection
An environment variable is a setting normally inherited or declared when a shell is started. You can use shells to set variables; the syntax varies but Bourne shells use:
$ VARNAME=new value $ export VARNAME or $ export VARNAME=new value
Each program started from that shell will have VARNAME set to new value. The names of environment variables are case-sensitive; by convention they are uppercase.
A shell variable is like an environment variable, except that it is not exported to new programs started from that shell. (You could export it, but normally you just write a shell initialisation script to set it in each shell.)
[edit] EDITOR
The editor program called by sudoedit, vipw, and other such programs when you tell them to edit a file.
Examples:
EDITOR=vi
EDITOR=emacs
Also see VISUAL.
[edit] HOME
The home directory of the user. Most programs use this shell variable to find your home, thus you can set this variable to override the setting in /etc/passwd for your home directory. This way, you can start programs that put dotfiles or other files in a different directory than your usual home directory.
In most shells, ~ refers to your home directory. In C shell, and some more recent versions of Bourne shell, ~tux always refers to the home directory of user tux as specified in /etc/passwd, while ~ (without a username after it) always refers to the value of HOME, even if it differs from your home directory in /etc/passwd.
[edit] LOGNAME
The name of the user. This is an easy way for a user to get own username. However, programs must not trust this variable because it can be set to an arbitrary value.
Both LOGNAME and USER should be set to the username.
Examples:
LOGNAME=tux
LOGNAME=puffy
[edit] MAIL
The location of incoming local email. When mail or another local email reader inherits this environment variable, it uses this variable to find the inbox.
Some users do not have email at their local Unix box, but instead use the Internet to access their mail server, in which case the MAIL environment variable is irrelevant.
Many users do not have MAIL set, in which case the email reader uses the default setting. The default value for user "tux" would be /var/mail/tux, which is where many systems deliver mail.
[edit] MAILCHECK
- This is a shell variable, not normally exported as an environment variable.
The frequency for which "bash" checks and alerts you for new local email.
[edit] PAGER
The pager called by man and other such programs when you tell them to view a file.
Examples:
PAGER=less
PAGER=more
[edit] PATH
A space or colon separated list of directories in which the shell searches for executables when a command is run without an absolute path. For example ls doesn't have an absolute path, but /bin/ls does).
Some systems set PATH using the system shell initialistation files, such as /etc/profile for Bourne shells. Some systems set PATH before this as part of the login procedure, for example in /etc/login.conf for OpenBSD systems. For example, a Linux box could set the PATH at login, then add /usr/X11R6/bin to the path using /etc/profile, then add /home/ambler/bin to the path using ~/.bash_profile.
The system boot scripts also set PATH. On some Linux boxes, the first command to set the path would seem to be in /etc/rc.d/rc.sysinit, which is one of the shell scripts invoked by the init process (inittab).
Examples:
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/home/puffy/bin
If this PATH is set and you type the shell command
$ uname -r
then the shell searches for the "uname" executable program. First it searches in /bin, then /sbin, then /usr/bin. If /bin/uname is an executable (which it should be), then the shell stops searching and runs it. If /home/puffy/bin/uname also is executable, it is not run, because the search never reached that directory.
Q: How do you remove, for example, "/usr/bin" from PATH set as above?
[edit] PS1
- This is a shell variable, not normally exported as an environment variable.
The bash and public domain ksh shells use this as the prompt string.
Things that can be put in the prompt string include \h (hostname), \u (username), \w (absolute pathname of working directory), \W (name of working directory w/o path), \d (date), \t (time).
On some Red Hat boxes, the primary prompt string is set in the /etc/bashrc file. The prompt is also set in /etc/profile, but the setting in bashrc seems to take precedence. A ~/.bashrc file runs /etc/bashrc, which sets the prompt. Because every instance of "bash" runs ~/.bashrc, the prompt also appears in X sessions started from a display manager such as "xdm".
On some Slackware boxes, the command line prompt is set in /etc/profile. The xterm and rxvt prompts are different. The prompt is not set for X sessions, but it would be if you write a ~/.bashrc to do that. Prompts are shell variables set from shell initialisation scripts. They are not xterm settings set by X resources such as /usr/X11R6/lib/X11/app-defaults/XTerm.
- A Practical Guide to Linux, by Mark G. Sobell and published by Addison-Wesley (1998), has more information on prompt strings at page 331.
[edit] PS2
- This is a shell variable, not normally exported as an environment variable.
The bash and public domain ksh shells use this as a secondary prompt string.
[edit] USER
This variable should have the same setting and purpose as LOGNAME.
[edit] VISUAL
This variable is used to specify the "visual" - screen-oriented - editor. Usually you'd want to set it to the same value as the EDITOR variable. I imagine that originally EDITOR would've be set to ed (a line-based editor) and VISUAL would've been set to vi (a screen-based editor), these days though (when people uses screens and not teletype-machines) there is no need to choose different editors for the two.
[edit] References
- environ(7) manual page (FreeBSD, NetBSD, OpenBSD)
- Sobell, Mark G. (1998), A Practical Guide to Linux. Addison-Wesley
- SSC (2000), "Bash Reference Card", http://www.digilife.be/quickreferences/QRC/Bash%20Quick%20Reference.pdf (updated link)