Environment Variables

Environment Variables are parameters kept by a shell. They modify the behaviour of programs.

To get a list of current environment variables:

printenv

A typical output may be:

PATH=/usr/local/bin:/usr/local/sbin:/opt/local/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/X11R6/bin:. MANPATH=/opt/local/share/man:/usr/share/man:/usr/local/share/man:/usr/X11/man:. USER=johndoe HOME=/Users/johndoe SHELL=/bin/zsh LC_ALL=en_GB.UTF-8 LANG=nl_NL.UTF-8 TERM=xterm-color HOSTNAME=mymacbookpro EDITOR=mate_wait

As you can see, this list contains a lot of important variables. By convention, all variable names are in CAPITAL LETTERS.

To find a particular value:

echo $LC_ALL

Setting them is easy:

export LC_ALL=en_US.UTF-8   # in sh, bash, or zsh shell setenv LC_ALL en_US.UTF-8   # in csh, or tcsh shells

Since modifying these variables is so easy, no program should rely on the content for security purposes.

Set Enviroment Variables in Shells
Most shells set a few standard variables, such as USER, HOME and PATH. However, these variables may be insufficient. The default PATH of tcsh is /usr/bin:/bin:/usr/sbin:/sbin (no /usr/local/bin, let alone /opt/local/bin).

To set the default variables, edit the configuration files for your scripts.

For ZSH, edit ~/.zprofile (.zshenv is another option, but this is also run for each and every invocation of ZSH, including scripts, so you may want to keep the zshenv file as short as possible for portability reasons):

if [ -x /usr/local/bin ]; then PATH="/usr/local/bin:$PATH" fi if [ -x /usr/local/share/man ]; then MANPATH="/usr/local/share/man:$MANPATH" fi

For bash and sh, edit ~/.profile:

if [ -x /usr/local/bin ]; then PATH="/usr/local/bin:$PATH" fi if [ -x /usr/local/share/man ]; then MANPATH="/usr/local/share/man:$MANPATH" fi

For csh and tcsh, edit ~/.login

if ( -d /usr/local/bin ) then setenv PATH /opt/local/bin:${PATH} endif if ( -d /usr/local/share/man ) then setenv MANPATH /usr/local/share/man:${MANPATH} endif

Inheriting Variables in Forked Programs
By default, a forked program inherits all environment variables, as well as regular variables:

For example: export LC_ALL=en_GB; aspell -c myreport.txt

or (using regular variables): LC_ALL=en_GB; aspell -c myreport.txt

Set Variables for Mac OS X Applications
Mac OS X programs started from the Finder to not inherit any environment variables. This means that programs that rely on environment variables do not work properly. This in particular applies to programs that rely on locale variables (LC_ALL, LANG, LC_TIME, LC_NUMERIC, etc.) for language customization, or programs that implicitly use the PATH variable to execute Unix scripts beyond the scenes. Those programs will by default no find any unix tools installed in /sw or /opt/local, but use the default programs in /bin or /usr/bin.

It is possible to set these variables using the configuration file ~/.MacOSX/environment.plist:

mkdir ~/.MacOSX defaults write ~/.MacOSX/environment LC_ALL en_GB.UTF-8 defaults write ~/.MacOSX/environment PATH "/usr/local/bin:/usr/local/sbin:/usr/local/texlive/2008/bin/universal-darwin:/opt/local/bin:/bin:/sbin:/usr/bin:/usr/sbin:/usr/X11R6/bin:/Developer/usr/bin"

To read these variables:

defaults read ~/.MacOSX/environment # or cat ~/.MacOSX/environment.plist

You need to log out and log in before these changes take effect.

Inheriting Variables as Sudo
By default, all programs that are run with superuser rights using sudo inherit all environment variables. However, this is considered a security risk, as a user may set the PATH, PERLLIB or SHELLOPTS variable before running certain scripts (especially Perl, Python or PHP scripts). Most current sudo installations by default block all environment variables. It is then possible to allow the few harmless options (such as EDITOR, DISPLAY and LC_ALL).

Here is an excerpt op my sudo configuration file, /etc/sudoers:

Defaults       env_reset Defaults       env_keep += "BLOCKSIZE" Defaults       env_keep += "COLORFGBG COLORTERM" Defaults       env_keep += "__CF_USER_TEXT_ENCODING" Defaults       env_keep += "CHARSET LANG LANGUAGE LC_ALL LC_COLLATE LC_CTYPE" Defaults       env_keep += "LC_MESSAGES LC_MONETARY LC_NUMERIC LC_TIME" Defaults       env_keep += "LINES COLUMNS" Defaults       env_keep += "LSCOLORS" Defaults       env_keep += "SSH_AUTH_SOCK" Defaults       env_keep += "TZ" Defaults       env_keep += "DISPLAY XAUTHORIZATION XAUTHORITY" Defaults       env_keep += "EDITOR VISUAL"