Section 5.15. Variables


[Page 184 (continued)]

5.15. Variables

The shell supports two kinds of variableslocal and environment variables. Both kinds of variables hold data in a string format. The main difference between them is that when a shell invokes a subshell, the child shell gets a copy of its parent shell's environment variables, but not its local variables. Environment variables are therefore used for transmitting useful information between parent shells and their children.

Every shell has a set of predefined environment variables, usually initialized by the startup files described in later chapters. Similarly, every shell has a set of predefined local variables that have special meanings to the shell. Other environment and local variables may be created as needed, and are particularly useful when writing scripts. Figure 5-11 is a list of the predefined environment variables that are common to all shells.


[Page 185]

Figure 5-11. Predefined shell variables.

Name

Meaning

$HOME

The full pathname of your home directory.

$PATH

A list of directories to search for commands.

$MAIL

The full pathname of your mailbox.

$USER

Your username.

$SHELL

The full pathname of your login shell.

$TERM

The type of your terminal.


The syntax for assigning variables differs between shells, but the way that you access them is the same; if you prepend a $ to the name of a variable, this token sequence is replaced by the shell with the value of the named variable.

To create a variable, simply assign it a value; a variable does not have to be declared. The details of how variables are assigned are left to the specific shell chapters, but for now it's enough to know that the syntax for assigning a variable in the Bash and the Korn shell is as follows:

variableName=value          ...place no spaces around the =. 


In the following example, I displayed the values of some common shell environment variables:

$ echo HOME = $HOME, PATH = $PATH       ...list two variables. HOME = /home/glass, PATH = /bin:/usr/bin:/usr/sbin $ echo MAIL = $MAIL                     ...list another. MAIL = /var/mail/glass $ echo USER = $USER, SHELL = $SHELL, TERM=$TERM USER = glass, SHELL = /bin/sh, TERM=vt100 $ _ 


The next example illustrates the difference between local and environment variables. I assigned values to two local variables and then made one of them an environment variable by using the shell export command. I then created a child shell and displayed the values of the variables that I had assigned in the parent shell. Note that the value of the environment variable was copied into the child shell, but the value of the local variable was not. Finally, I typed a Control-D to terminate the child shell and restart the parent shell, and then displayed the original variables:

$ firstname=Graham               ..set a local variable. $ lastname=Glass                ...set another local variable. $ echo $firstname $lastname     ...display their values. 
[Page 186]
Graham Glass $ export lastname ...make "lastname" an environment var. $ sh ...start a child shell; the parent sleeps. $ echo $firstname $lastname ...display values again. Glass ...note that firstname wasn't copied. $ ^D ...terminate child; the parent awakens. $ echo $firstname $lastname ...they remain unchanged. Graham Glass $ _


There are several common built-in variables that have a special meaning (Figure 5-12).

Figure 5-12. Special built-in shell variables.

Name

Meaning

$$

The process ID of the shell.

$0

The name of the shell script (if applicable).

$1..$9

$n refers to the nth command-line argument (if applicable).

$*

A list of all the command-line arguments.


The first special variable is especially useful for creating temporary filenames, and the rest are handy for accessing command-line arguments in shell scripts. Here's an example that illustrates all of the common special variables:

$ cat script.sh                              ...list the script. echo the name of this script is $0 echo the first argument is $1 echo a list of all the arguments is $* echo this script places the date into a temporary file echo called $1.$$ date > $1.$$      # redirect the output of date. ls $1.$$          # list the file. rm $1.$$          # remove the file. $ ./script.sh paul ringo george john        ...execute it. the name of this script is script.sh the first argument is paul a list of all the arguments is paul ringo george john this script places the date into a temporary file called paul.24321 paul.24321 $ _ 





Linux for Programmers and Users
Linux for Programmers and Users
ISBN: 0131857487
EAN: 2147483647
Year: 2007
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net