Understanding Shell Variables in Linux
Shell variables are capitalized by convention. The shell maintains two lists of variables:
- Those local to the current shell
- Those global to all shells (environment variables).
Use the set and env commands to display local and environmental variables, respectively. The following is a partial output of the set and env statements. Many variables appear in both the local and environment variable list.
$ set AB2_DEFAULTSERVER=http://docs.sun.com/ CUE_HOSTNAME=sunray10 DISPLAY=:46.0 DOMAIN=renegades.Central.Sun.COM DOMAIN_COUNT=1 DTSOURCEPROFILE=true DTUSERSESSION=milner-sunray10-46 DTXSERVERLOCATION=local EDITOR=vi ERRNO=25 FCEDIT=/bin/ed LANG=C LOGNAME=milner LPDEST=hutchence MAIL=/var/mail/milner MAILCHECK=600 MANPATH=/usr/dt/man:/usr/man:/usr/openwin/share/man:/usr/man:/usr/openwin /share/man:/usr/dt/man:/usr/dist/local/man/5.7 PRINTER=hutchence PS1=’$ ’ PS2=’> ’ PS3=’#? ’ PS4=’+ ’ PWD=/home/milner/K-Shell-Course/InstructorGuide/Examples SHELL=/bin/ksh TZ=US/Mountain ...
$ env DTSOURCEPROFILE=true DOMAIN=renegades.Central.Sun.COM DTUSERSESSION=milner-sunray10-46 EDITOR=vi LOGNAME=milner MAIL=/var/mail/milner CUE_HOSTNAME=sunray10 PRINTER=hutchence DISPLAY=:46.0 TERM=dtterm TZ=US/Mountain LPDEST=hutchence DOMAIN_COUNT=1 ...
Variable names can contain uppercase or lowercase letters, digits, and underscores. Variable names cannot begin with a digit. Some of the boot scripts show mixed-case variable names. Be sure to use meaningful variable names.
Creating Variables in the Shell
To set a variable in the shell, use the syntax:
Do not place spaces around the = sign. If the value contains spaces or special characters, use single or double quotes; for example:
$ name="Susan B. Anthony"
To display the value of a shell variable, use the echo command and place a $ immediately in front of the variable name. When you have finished a variable, you can unset the value and release the resources with the unset command; for example:
$ MYNUM=21 $ MACHTYPE=sparc $ echo $MYNUM 21 $ echo $MYNUM $MACHTYPE 21 sparc $ unset MYNUM $ echo $MYNUM $MACHTYPE sparc
In the following example, notice how variable substitution happens before file name expansion:
# VAR="*" # ls $VAR hsperfdata_noaccess: 805 hsperfdata_root: 3101 # >-l # ls $VAR hsperfdata_noaccess: total 64 -rw------- 1 noaccess noaccess 32768 Nov 13 15:43 805 hsperfdata_root: total 64 -rw------- 1 root root 32768 Nov 23 15:27 3101
The $VAR variable is first resolved and then passed to the ls command.
Exporting Variables to Subshells
Variables created in a shell are not known outside that shell. These variables are local variables. They are “local to” (known only in) the current shell. Subshells of a shell do not automatically inherit the variables of their parent shell.Only the variables that are exported are passed onto subshells of the parent shell. Variables that are exported are referred to as environment variables.
To export a variable (with its current value) to subshells of a shell, use:
export variable_name1 variable_name2 ...
The following example creates two variables in the current shell, x and name. The name variable is then exported to be inherited by any subshells of the parent.
$ x=25 $ name="Marion Morrison" $ echo $x $name 25 Marion Morrison $ export name
A subshell is created by invoking a new Korn shell using the ksh command.
$ ksh $ print $x $ print $name Marion Morrison $ export name="Marlena Dietrich" $ print $name Marlena Dietrich $ exit
When you try to access the value of the variable x using the echo command, no output is shown because the child shell does not know the variable x. When you access the value of the variable name, the value of the variable is output because this variable was exported in the parent shell. Changing the contents of the name in the subshell has no effect on the value of the name variable in the parent.
$ echo $name Marion Morrison
The shell knows about and uses certain variables. Use care when modifying the values for these variables. Before modifying a value, know how the shell uses the variable and how you might affect your interaction with the shell. To learn about variables not listed in this module, read the man pages for sh or ksh, depending on your choice for an interactive shell. The table below describes the shell variables.
|HOME||User’s login directory path.|
|IFS||Internal field separators, which hold a characters used as inter-field separators. By default, this string includes a space, a tab, and a new line. Don’t change this string.|
|LOGNAME||LOGNAME User’s login name.|
|MAILCHECK||How often, in seconds, the mail daemon checks for mail.|
|OPTIND||getopts statement uses this variable during parsing of options on command line.|
|PATH||Search path for commands.|
|PS1||The prompt. It defaults to $.|
|PS2||Prompt used when command line continues. Defaults to >.|
|PS3||Prompt used within a select statement. Defaults to #?.|
|PS4||Prompt used by the shell debug utility. Defaults to +.|
|PWD||Present working directory.|
|SHELL||Shell defined for the user in the passwd file.|
|TERM||Terminal type, which is used by the vi editor and other commands.|
Special Shell Variables
Several shell variables are available to users. The shell sets the variable value at a process creation or termination.
The $ shell variable is available to users. It contains the current process ID number. Use this variable to create file names that are unlikely to already exist.
$ echo $$ 8873 $ sh $ echo $$ 17716 $ exit $ echo $$ 8873
The exit status is the integer value of the last executed command (program, script, or shell statement). The ? variable provides the exit status of the most recent foreground process. It:
- Determines if the process ran successfully.
- Sets the ? variable to 0 if a process succeeded and to a nonzero value if it didn’t succeed.
Think of this as equating success with zero errors occurring and equating failure with one or more errors occurring or the command not being able to do what was requested:
$ grep "root" /etc/passwd root:x:0:1:Super-User:/:/sbin/sh $ echo $? 0 $ grep "rot" /etc/passwd $ echo $? 1
Background Process Identification
A shell allows you to run a command in the background using the & operator. To find the process identification number (PID) of the last background job started, echo the ! variable.
$ date & 1175 $ Mon Oct 12 06:23:11 IST 2009 $ echo $! 1175