5.5. ENVIRONMENT Variables

5.5.1. Setting an ENV variable

Each user can set up their own environment for their own needs. The environment is a group of variables to which each is assigned some value. The most common ones are assigned during the user login with /etc/login.conf. A user's ~/.profile can add or modify environment variables.

Use printenv to check the current values of a shell's environment. Here is a partial display of what may be seen:

% printenv
...
SHELL=/usr/local/bin/bash
TERM=xterm
EDITOR=vi
PAGER=more
FTP_PASSIVE_MODE=YES
...

A specific environment variable can be checked as follows:

% echo $PATH
PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin:
/usr/X11R6/bin:/home/rpratt/bin

Note: Output is shown on two lines for clarity and to fit to the page. It is all one line.

5.5.2. Scope of ENV variables

Environment variables can also be used to share common information between scripts and applications. The scope of the ENV variable depends where it is set.

5.5.2.1. Scripts and applications

If the ENV variables are set within a script, they only affect things while that script is running. This can be used to change the value of one set on a global basis. For example, to display the date in GMT and not affect anything else:

#!/bin/sh

TZ=GMT
export TZ

date

If another script or application is invoked from within the a script then it will also inherit the ENV variables.

5.5.2.2. Xterm and console

If the following commands are issued from an xterm or console, then any application or script ran in that xterm or console will utilize the same ENV variables:

% CVSROOT=:pserver:anoncvs:anoncvs@anoncvs.fr.freebsd.org:/home/ncvs
% export CVSROOT

Environment variables can be temporarily set for one script or application:

% env SETTING1='setting1' sh script.sh
% env TZ=GMT date

5.5.2.3. All user scripts/applications

The user .profile can be used to set ENV variables that affect all user applications and scripts. This excerpt from .profile shows setting the ENV variables needed to build the documentation using Docproj:

#added for docproj
SGML_ROOT=/usr/local/share/sgml
SGML_CATALOG_FILES=${SGML_ROOT}/jade/catalog
SGML_CATALOG_FILES=${SGML_ROOT}/iso8879/catalog:$SGML_CATALOG_FILES
SGML_CATALOG_FILES=${SGML_ROOT}/html/catalog:$SGML_CATALOG_FILES
SGML_CATALOG_FILES=${SGML_ROOT}/docbook/catalog:$SGML_CATALOG_FILES
export SGML_CATALOG_FILES

Note: It may be necessary to invoke a shell as a login shell in order for it to read the .profile

5.5.2.4. ENV variables on a global scale

Instead of hard-coding everything in /etc/rc.conf, something like this could be done:

#!/bin/sh
#<title>Stop/Restart ifconfig for interface</title>

# Set up interface to hub
export ifconfig_vr0="inet 192.168.0.50  netmask 255.255.255.0"

# Select gateway or node (not both)
gateway_enable="NO"             # Set to YES if this host will be a gateway.
defaultrouter="NO"              # Set to default gateway (or NO).

# Restart the interface
/etc/rc.d/netif stop vr0
/etc/rc.d/netif start vr0