Understanding Unix shells and environment variables, Part 2
Examine and customize your Unix environment
Summary
This month, Mo Budlong explores some of the variables and commands used by the Unix shell. Along the way, he'll help you improve your understanding of how the shell, the environment, and external commands work together. (1,500 words)
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
Unix shells and environment variables: Read the whole series!
|
|
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.
For example, PS1 , listed above as an environment variable, is the prompt displayed on the screen when the shell is waiting for a new command. Another shell variable, PS2 , contains the prompt to be used when a command is begun but not completed before Enter is pressed. To see the prompt in use, type the commands below. The first echoes the $PS2 prompt to the screen. Then a new command is started with an opening parenthesis. The user presses Enter immediately and the shell waits for a command and a closing parenthesis. The shell displays the > prompt to indicate that it is waiting for more input. The command is entered and Enter is pressed. Once again, the > prompt is displayed, because the user has not yet closed the open parenthesis. Finally, the user types ) and presses Enter, ending the command.
$ echo $PS2
>
$ (
> cat /etc/passwd|grep mjb
> )
$
You can create a more graphic version of this by adding a command to change the $PS2 prompt. In the following example, the value of the $PS2 prompt is changed and the same command sequence is entered. The $PS2 prompt is reset.
$ echo $PS2
>
$ PS2="more please> "
$ (
more please > cat /etc/passwd|grep mjb
more please > )
$ PS2="> "
# echo $PS2
>
$
Why does the PS2 prompt have a value if it is not in the environment? Look at the printenv listing and you will not see an entry for PS2 .
$ 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]\$
The shell sets up some default shell variables; PS2 is one of them. Other useful shell variables that are set or used in the Korn shell are:
_ (underscore) -- When an external command is executed by the shell, this is set in the environment of the new process to the path of the executed command. In interactive use, this parameter is also set in the parent shell to the last word of the previous command.
COLUMNS -- The number of columns on the terminal or window.
ENV -- If this parameter is found to be set after any profile files are executed, the expanded value is used as a shell startup file. It typically contains function and alias definitions.
ERRNO -- Integer value of the shell's errno variable -- this indicates the reason the last system call failed.
HISTFILE -- The name of the file used to store history. When assigned, history is loaded from the specified file. Multiple invocations of a shell running on the same machine will share history if their HISTFILE parameters all point to the same file. If HISTFILE isn't set, the default history file is $HOME/.sh_history .
HISTSIZE -- The number of commands normally stored in the history file. Default value is 128.
IFS -- Internal field separator, used during substitution and by the read command to split values into distinct arguments; normally set to space, tab, and newline.
LINENO -- The line number of the function or shell script that is being executed. This variable is useful for debugging shell scripts. Just add an echo $LINENO at various points and you should be able to determine your location within a script.
LINES -- Set to the number of lines on the terminal or window.
PPID -- The process ID of the shell's parent. A read-only variable.
PATH -- A colon-separated list of directories that are searched when seeking commands.
PS1 -- The primary prompt for interactive shells.
PS2 -- Secondary prompt string; default value is > . Used when more input is needed to complete a command.
PWD -- The current working directory. This may be unset or null if shell does not know where it is.
RANDOM -- A simple random number generator. Every time RANDOM is referenced, it is assigned the next number in a random number series. The point in the series can be set by assigning a number to RANDOM .
REPLY -- Default parameter for the read command if no names are given.
SECONDS -- The number of seconds since the shell started or, if the parameter has been assigned an integer value, the number of seconds since the assignment plus the value that was assigned.
TMOUT -- If set to a positive integer in an interactive shell, it specifies the maximum number of seconds the shell will wait for input after printing the primary prompt (PS1 ). If this time is exceeded, the shell exits.
TMPDIR -- Where the directory shell temporary files are created. If this parameter is not set, or does not contain the absolute path of a directory, temporary files are created in /tmp .
The C shell uses variables with similar but lowercase names, such as prompt1 , prompt2 , path , home , and so on.
Other interesting variables are the locale setting variables. These variables are LC_ALL , LC_CTYPE , LC_COLLATE , and LC_MESSAGES . LC_ALL effectively overrides the values for the other three LC variables; you can set them independently by not setting LC_ALL .
LC_ALL -- Determines the locale to be used to override any previously set values.
LC_COLLATE -- Defines the collating sequence to use when sorting.
LC_CTYPE -- Determines the locale for the interpretation of a sequence of bytes.
LC_MESSAGES -- Determines the language in which messages should be written.
LC_ALL can be used to change the language for the system. Try the following sequence of commands below to see these in action. The language is changed to French (fr ) and grep is invoked with an illegal option -x . The error message appears in French. The LC_ALL is set to Spanish (español, thus es ) and the error and error message are repeated. Finally LC_ALL is unset and the error returns in English.
$ export LC_ALL=fr
$ grep -x
Usage: grep [OPTION]...PATRON [FICHIER]
Pour en savoir davantage, faites: 'grep --help'
$ LC_ALL=es
$ grep -x
Modo de empoleo: grep [OPCION]...PATRON [FICHERO]
Pruebe 'grep --help' para mas informacion
$ unset LC_ALL
$ grep -x
Usage: grep [OPTION]...PATTERN [FILE]
Try 'grep --help' for more information.
$
End of article.
Contact
us for a free consultation. |