Guide to Unix/Environment Variables

From Wikibooks, open books for an open world
Jump to navigation Jump to search

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.)






EDITOR[edit | edit source]

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.

HOME[edit | edit source]

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.

LOGNAME[edit | edit source]

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=sudhir

MAIL[edit | edit source]

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.

MAILCHECK[edit | edit source]

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.

MANPATH[edit | edit source]

The path used by the man(1) command to search for manual pages. The MANPATH environment variable is formatted with ':' separators just like the PATH environment variable.

PAGER[edit | edit source]

The pager called by man and other such programs when you tell them to view a file.

Examples:

PAGER=less
PAGER=more

PATH[edit | edit source]

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 initialization 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.

In most cases, you only append to the PATH shell variable before exporting it to the PATH environment variable. To delete a directory from your search path, you must reassign the entire PATH variable to a new, shorter string, and often this takes a great deal of typing or some cut and paste operations.

PS1[edit | edit source]

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.

PS2[edit | edit source]

This is a Primary shell variable, not normally exported as an environment variable.

The bash and public domain ksh shells use this as a secondary prompt string.

SHELL[edit | edit source]

The name of the user's default shell, such as /bin/sh, /bin/bash, or /bin/tcsh. See also Explanations/Choice of Shell.

USER[edit | edit source]

This variable should have the same setting and purpose as LOGNAME.

VISUAL[edit | edit source]

This variable is used to specify the "visual" - screen-oriented - editor. Generally you will want to set it to the same value as the EDITOR variable. Originally EDITOR would have be set to ed (a line-based editor) and VISUAL would've been set to vi (a screen-based editor). These days, you're unlikely to ever find yourself using a teletype as your terminal, so there is no need to choose different editors for the two. Nevertheless, it is useful to have both set: Many programs, including less and crontab, will invoke VISUAL to edit a file, falling back to EDITOR if VISUAL is not set - but others invoke EDITOR directly.

Examples:

VISUAL=mg
VISUAL=vi

See also EDITOR.

References[edit | edit source]