Previous | Table of Contents | Next |
The variables that you have examined so far have all been user variables. A user variable is one that the user can manually set and reset.
In this section, you look at shell variables, which are variables that the shell sets during initialization and uses internally. Users can modify the value of these variables.
Table 7.2 gives a partial list of these shell variables. In addition to these variables, I cover several special variables in the section Variable Substitution in Chapter 8. Unless noted, all the variables given in Table 7.2 are available in sh, ksh, and bash.
Variable | Description |
---|---|
PWD | Indicates the current working directory as set by the cd command. |
UID | Expands to the numeric user ID of the current user, initialized at shell startup. |
SHLVL | Increments by one each time an instance of bash is started. This variable is useful for determining whether the built-in exit command ends the current session. |
REPLY | Expands to the last input line read by the read built-in command when it is given no arguments. This variable is not available in sh. |
RANDOM | Generates a random integer between 0 and 32,767 each time it is referenced. You can initialize the sequence of random numbers by assigning a value to $RANDOM. If $RANDOM is unset, it loses its special properties, even if it is subsequently reset. This variable is not available in sh. |
SECONDS | Each time this parameter is referenced, it returns the number of seconds since shell invocation. If a value is assigned to $SECONDS, the value returned on subsequent references is the number of seconds since the assignment plus the value assigned. If $SECONDS is unset, it loses its special properties, even if it is subsequently reset. This variable is not available in sh. |
IFS | Indicates the Internal Field Separator that is used by the parser for word splitting after expansion. $IFS is also used to split lines into words with the read built-in command. The default value is the string, \t\n, where is the space character, \t is the tab character, and \n is the newline character. |
PATH | Indicates search path for commands. It is a colon-separated list of directories in which the shell looks for commands. A common value is |
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/ucb | |
HOME | Indicates the home directory of the current user: the default argument for the cd built-in command. |
In this chapter, you looked at using variables for shell script programming. You learned how to define, access, and unset scalar and array variables. You also looked at special classes of variables known as environment variables and shell variables.
In the following chapters, you look at how variables are used to achieve a greater degree of flexibility and clarity in shell scripts. As you read, continue learning about shell programming until using variables becomes second nature to you.
$ adams[0]=hitchhikers_guide $ adams[1]=restaurant $ adams[3]=thanks_for_all_the_fish $ adams[42]=life_universe_everything $ adams[5]=mostly_harmless
Previous | Table of Contents | Next |