Understanding Unix shells and environment variables, Part 2

Unix Insider |  Operating Systems

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,
mjb.

$ 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 /etc/profile and $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.cshrc, then /etc/csh.login, then an entire raft of files in your home directory, such as ~/.cshrc, ~/.history, ~/.login, and, finally, ~/.cshdirs.

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 printenv or 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.

Join us:
Facebook

Twitter

Pinterest

Tumblr

LinkedIn

Google+

Operating SystemsWhite Papers & Webcasts

See more White Papers | Webcasts

Answers - Powered by ITworld

Ask a Question