Text-only Table of Contents (frame/ no frame)
(10) Preset Variables Previous Top Next

Preset Shell Variables

Several special variables are used by the system -- you can use these, but may not be able to change them. The special variables use uppercase names, or punctuation characters. Some variables are set by the login process and inherited by the shell (e.g. $USER), while others are used only by the shell.
Try running set or env
These are some of the more commonly used ones:

Login environment

$USER, $LOGNAME
Preset to the currently logged-in username.
$PATH
The list of directories that will be searched for external commands. You can change this in a script to make sure you get the programs you intend, and don't accidentally get other versions which might have been installed.
$TERM
The terminal type in which the shell session is currently executing. Usually "xterm" or "vt100". Many programs need to know this to figure out what special character sequences to send to achieve special effects.
$PAGER
If set, this contains the name of the program which the user prefers to use for text file viewing. Usually set to "more" or "less" or something similar. Many programs which need to present multipage information to the user will respect this setting (e.g. man). This isn't actually used by the shell itself, but shell scripts should honour it if they need to page output to the user.
$EDITOR
If set, this contains the name of the program which the user prefers to use for text file editing. A program which needs to have the user manually edit a file might choose to start up this program instead of some built-in default (e.g. "crontab -e". This also determines the default command-line-editing behaviour in interactive shells.

Shell internal settings

$PWD
Always set the current working directory (readonly)
$OLDPWD
The previous directory (before the most recent cd command). However, changing directories in a script is often dangerous.
$? (readonly)
Set to the exit status of the last command run, so you can test success or failure. Every command resets this so it must be saved immediately if you want to use it later.
$-
Set to the currently set options flags.
$IFS
Internal Field Separators: the set of characters (normally space and tab) which are used to parse a command line into separate arguments. This may be set by the user for special purposes, but things get very confusing if it isn't changed back.

Process ID variables

$$ (readonly)
Set to the process ID of the current shell - useful in making unique temporary files, e.g. /tmp/$0.$$
$PPID (readonly)
Set to the process ID of the parent process of this shell - useful for discovering how the script was called.
$! (readonly)
Set to the process ID of the last command started in background - useful for checking on background processes.

ksh/bash additional features

$SECONDS (readonly)
Integer number of seconds since this shell was started. Can be used for timing commands.
$RANDOM
Every time it is valuated, $RANDOM returns a random integer in the range 0-32k. RANDOM may be set to "seed" the random number generator.
$LINENO (readonly)
Always evaluates to the current line number of the script being executed - useful for debugging.

Previous Top Next


special-vars.src  last modified Mar 26, 2010 Introduction Table of Contents
(frame/no frame)
Printable
(single file)
© Dartmouth College