Sams Teach Yourself Shell Programming in 24 Hours
(Publisher: Macmillan Computer Publishing)
Author(s): Sriranga Veeraraghavan
ISBN: 0672314819
Publication Date: 01/01/99

Previous Table of Contents Next


Shell Variables


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.

Table 7.2 Shell Variables

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.

Summary

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.

Questions

1.  Which of the following are valid variable names?
a.  _FRUIT_BASKET
b.  1_APPLE_A_DAY
c.  FOUR-SCORE&7YEARS_AGO
d.  Variable
2.  Is the following sequence of array assignments valid in sh, ksh, and bash?
$ adams[0]=hitchhikers_guide
$ adams[1]=restaurant
$ adams[3]=thanks_for_all_the_fish
$ adams[42]=life_universe_everything
$ adams[5]=mostly_harmless
3.  Given the preceding array assignments, how would you access the array item at index 5 in the array adams? How about every item in the array?
4.  What is the difference between an environment variable and a local variable?

Terms

Scalar Variable
A scalar variable can hold only one value at a time.
Array Variable
An array variable is a mechanism available in bash and ksh for grouping scalar variables together. The scalar variables stored in an array are accessed using a single name in conjunction with a number. This number is referred to as an index.
Local Variable
A local variable is a variable that is present within the current instance of the shell. It is not available to programs that are started by the shell.
Environment Variable
An environment variable is a variable that is available to any program that is started by the shell.
Shell Variable
A shell variable is a special variable that is set by the shell and is required by the shell in order to function correctly.
Exporting
A variable is placed in the environment by exporting it using the export command.


Previous Table of Contents Next