Internal Variables
Builtin variables
==================================
$BASH
the path to the Bash binary itself bash$ echo $BASH
$BASH_ENV
an environmental variable pointing to a Bash startup file to be read when a script is invoked
$BASH_SUBSHELL
a variable indicating the subshell level. This is a new addition to Bash, version 3.
$BASH_VERSINFO[n]
a 6-element array containing version information about the installed release of Bash. This is similar to $BASH_VERSION, below, but a bit more detailed.
$BASH_VERSION
the version of Bash installed on the system
$DIRSTACK
the top value in the directory stack (affected by pushd and popd)
This builtin variable corresponds to the dirs command, however dirs shows the entire contents of the directory stack.
$EDITOR
the default editor invoked by a script, usually vi or emacs.
$EUID
"effective" user ID number
Identification number of whatever identity the current user has assumed, perhaps by means of su.
$FUNCNAME
name of the current function
$GLOBIGNORE
A list of filename patterns to be excluded from matching in globbing.
$GROUPS
groups current user belongs to
$HOME
home directory of the user, usually /home/username (see Example 9-15)
$HOSTNAME
The hostname command assigns the system name at bootup in an init script. However, the gethostname() function sets the Bash internal variable $HOSTNAME. See also Example 9-15.
$HOSTTYPE
host type
$IFS
internal field separator
$IGNOREEOF
ignore EOF: how many end-of-files (control-D) the shell will ignore before logging out.
$LC_COLLATE
Often set in the .bashrc or /etc/profile files, this variable controls collation order in filename expansion and pattern matching. If mishandled, LC_COLLATE can cause unexpected results in filename globbing.
$LC_CTYPE
This internal variable controls character interpretation in globbing and pattern matching.
$LINENO
This variable is the line number of the shell script in which this variable appears. It has significance only within the script in which it appears, and is chiefly useful for debugging purposes.
$MACHTYPE
machine type
$OLDPWD
old working directory ("OLD-print-working-directory", previous directory you were in)
$OSTYPE
operating system type
$PATH
path to binaries, usually /usr/bin/, /usr/X11R6/bin/, /usr/local/bin, etc.
$PIPESTATUS
Array variable holding exit status(es) of last executed foreground pipe. Interestingly enough, this does not necessarily give the same result as the exit status of the last executed command.
$PIPESTATUS is a "volatile" variable.
It needs to be captured immediately after the pipe in question, before any other command intervenes.
$PPID
The $PPID of a process is the process ID (pid) of its parent process. [25]
$PROMPT_COMMAND
A variable holding a command to be executed just before the primary prompt, $PS1 is to be displayed.
$PS1
This is the main prompt, seen at the command line.
$PS2
The secondary prompt, seen when additional input is expected. It displays as ">".
$PS3
The tertiary prompt, displayed in a select loop (see Example 10-29).
$PS4
The quartenary prompt, shown at the beginning of each line of output when invoking a script with the -x option. It displays as "+".
$PWD
working directory (directory you are in at the time)
$REPLY
The default value when a variable is not supplied to read. Also applicable to select menus,
but only supplies the item number of the variable chosen, not the value of the variable itself.
$SECONDS
The number of seconds the script has been running.
$SHELLOPTS
the list of enabled shell options, a readonly variable bash$ echo $SHELLOPTS
braceexpand:hashall:histexpand:monitor:history:interactive-comments:emacs
$SHLVL
Shell level, how deeply Bash is nested. If, at the command line,
$SHLVL is 1, then in a script it will increment to 2.
$TMOUT
If the $TMOUT environmental variable is set to a non-zero value time,
then the shell prompt will time out after $time seconds. This will cause a logout.
$UID
user ID number
Positional Parameters
==================================
$0, $1, $2, etc.
positional parameters, passed from command line to script, passed to a function, or set to a variable (see Example 4-5 and Example 14-15)
$#
number of command line arguments [26] or positional parameters (see Example 33-2)
$*
All of the positional parameters, seen as a single word
"$*" must be quoted.
$@
Same as $*, but each parameter is a quoted string, that is, the parameters are passed on intact, without interpretation or expansion. This means, among other things, that each parameter in the argument list is seen as a separate word.
Of course, "$@" should be quoted.
Other Special Parameters
==================================
$-
Flags passed to script (using set). See Example 14-15.
$!
PID (process ID) of last job run in background
$?
Exit status of a command, function, or the script itself (see Example 23-7)
$$
Process ID of the script itself. The $$ variable often finds use in scripts to construct "unique" temp file names (see Example A-13, Example 29-6, Example 15-28, and Example 14-26). This is usually simpler than invoking mktemp.