Previous Table of Contents Next


Built-In Shell Variables

All three shells have a set of single-character variables that are set initially by the shell, as shown in Table 16-5. You can use these variables to access words in variables and return other information about the variable. These variables are used differently in the C shell than they are in the Bourne and Korn shells.

Table 16-5 Variables Initialized by Shell

Variable Explanation
$* Bourne or Korn shell: List the value of all command-line parameters. This variable is useful only in scripts because the login shell has no arguments associated with it.
C Shell: Not used. Use $argv instead.
$# Bourne or Korn shell: Return the number of command-line arguments (in decimal). Useful only in scripts.
C shell: Count the number of words (in decimal) in a variable array.
$? Bourne or Korn shell: Return the exit status (in decimal) of the last command executed. Most commands return a zero exit status if they complete successfully; otherwise a non-zero exit status is returned. This variable is set after each command is executed.
C shell: Check to see if a variable of that name has been set.
$$ All shells: Return the process ID (PID) number of the current shell (in decimal).
$! Bourne or Korn shell: Return the process number (in decimal) of the last process run in the background.

For the Bourne and Korn shells, you can use the $* variable to list the values of command-line arguments within a script, and the $# variable to hold the number of arguments. In the following example, the shell expands the $* variable to list all of the command-line arguments to the script:

#!/bin/sh

echo $#
for var in $*
do
     echo $var
done

If you named the script tryit and executed it with three arguments, it would echo the value of arguments from $#, and then display the list of arguments from $*, one on each line. The input string may contain quotes. Refer to the sh(1) manual page for information about how quoted strings are interpreted.

$ tryit one two three
3
one
two
three
$

For the C shell, the $#variable command holds the number of words in a variable array, as shown in the following example:

oak% set var = (a b c)
oak% echo $#var
3
oak%

For the Bourne shell, the $? variable displays exit status for the last command executed; it displays this information in the same way that the C shell status variable does. Refer to the section "Exit Status" on page 348 for an example.

For the C shell, you can use the variable $?variable to test whether a variable is set. $?variable returns a 1 if a named variable exists, or 0 if the named variable does not exist.

oak% set var="a b c"
oak% echo $?var
1
oak% unset var
oak% echo $?var
Ø
oak%


NOTE:  The numbers returned by $?variable are the opposite from status numbers, where 0 is success and 1 is failure.

For all shells, the $$ variable returns the PID number of the current shell process, as shown in the following examples. Because process numbers are unique, you can use this string to generate unique temporary file names. For example, when a script assigns a filename of tmp.$$, that file rarely is confused with another file.


NOTE:  If you are concerned about generating unique filenames, you can use the format feature of the date(1) command to generate a temporary filename that includes both a process ID and a time and date stamp.
oak% echo $$
364
oak% sh
$ echo $$
392
$


Previous Table of Contents Next