October 12, 2001, 1:18 PM — Unix shells come with variables that are used by the shell or related commands. In addition to variables that you create, the shell itself requires or takes advantage of variables that can be set up for it.
When you first log in to a Unix system, the
/etc/passwd file contains the name of the shell that is to be run for you. This appears in the last field of the password file. To see yours, type
cat /etc/passwd and pipe the result through
grep looking for your userid. In the example below I have used my id,
$ cat /etc/passwd|grep mjb mjb:500:500::/home/mjb:/bin/ksh
In this example, my logon runs the Korn shell. This shell reads and executes any existing file named
/etc/profile, which a system administrator has programmed for basic setup actions required for all users. After I execute
/etc/profile, I execute
$HOME/.profile. This is set up to contain my own environment. Both
$HOME/.profile set environment variables. The Bourne shell works in a similar fashion.
The C shell also takes a similar approach, but uses more files. It runs
/etc/csh.login, then an entire raft of files in your home directory, such as
~/.login, and, finally,
Regardless of the approach, the result is an environment in which the user will run, including environment variables. You can see your environment variables by using
env. The following is a short example of the output.
$ printenv USERNAME= HISTSIZE=1000 HOSTNAME=my.system.com LOGNAME=mjb MAIL=/var/spool/mail/mjb TERM=xterm PATH=/usr/bin:/bin:/usr/local/bin:/usr/bin/X11:/home/mjb/bin HOME=/home/mjb SHELL=/bin/ksh PS1=[\u@\h \W]\$
Shells also use variables that are not part of the environment. For a description of the difference between shell and environment variables, see last month's column.