3 2 Setting and Displaying Variables

   

3 2 Setting and Displaying Variables

When using the POSIX shell, you can set a variable at the command prompt just by entering a variable name followed by an " = " sign and the value you want to assign to the variable. It is a convention to name all user -created variables with uppercase letters, but lowercase letters can be used if needed. Also, a variable name can start with characters of the alphabet only, not with numbers . For example, VAR3 is a legal name for a variable while 3VAR is not. Below is the process of setting a variable.

 $  VAR3=TestVar  $ 

The command prompt appears without any message. Note that there is no space character on either side of the " = " symbol. If you place a space around the = sign, the shell interprets it as a command and displays an error message, as no command with the name VAR3 exists on HP-UX.

The echo command is used to view the value of a particular shell variable. To view the value of our newly created shell variable, use the following method.

 $  echo $VAR3  TestVar $ 

Notice that we have used a $ symbol to start the variable name. If we don't use the $ symbol, the echo command displays whatever it gets on the command line as is. The $ symbol tells the echo command that the argument is a variable, not a simple text string. The result of using the above command without $ is as follows .

 $  echo VAR3  VAR3 $ 

As you can see, the echo command has displayed its argument text, not the variable value.

Listing All Variables

If you want to list all variables known to your current shell, use the set command.

 $  set  EDITOR=vi EPC_DISABLED=TRUE ERASE=^H FCEDIT=/usr/bin/ed HISTFILE=/home/root/.sh_history HISTSIZE=400 HOME=/home/boota INTR=^C LINENO=1 LOGNAME=boota MAIL=/var/mail/boota MAILCHECK=600 MANPATH=/usr/share/man/%L:/usr/share/man:/usr/contrib/man:/ u sr/local/man/%L:/usr/local/man NAME=12 OPTIND=1 PATH=/usr/sbin:/baan/bse/bin:/usr/bin:/usr/ccs/bin:/usr/ contrib/bin:/usr/bin/X11:/usr/contrib/bin/X11:/opt/perf/ bin:/u sr/sbin:/sbin PPID=26709 PS1='boota on myhp $PWD => ' PS2='> ' PS3='#? ' PS4='+ ' PPID=26709 SHELL=/sbin/sh TERM=vt100 TMOUT=0 TZ=EST5EDT VAR3=TestVar _=set $ 

This list will change from system to system. It also depends on what applications are running on the system, as applications also set their environment variables. We will discuss some of the common variables in the next pages.

Variable Containing More Than One Word

Often a variable has a value that contains space characters in it. If you try to set a variable containing spaces in the normal way, you will get an error message as follows.

 $  NAME=Mike Ron  sh: Ron:  not found. $ 

The shell thought that you were setting a variable NAME with value Mike while Ron is a UNIX command. The shell then tried to execute this command and failed. To set variables containing multiple words, we use single or double quotes.

 $  NAME="Mike Ron"  $ $  echo $NAME  Mike Ron $ 

Single quotes may also be used.

 $  NAME='Mike Ron'  $ 

There is a slight difference between single- and double-quote characters that I will soon elaborate on.

The echo command can be used to display a variable and additional text at the same time. For example, just after displaying the NAME variable, we want to display the number 7 . What if we use command echo $NAME7 ?

 $  echo $NAME7  sh: NAME7: Parameter not set. $ 

The shell actually started looking for a variable name NAME7 instead of NAME but could not find it. To avoid this ambiguity, we use {} to separate a variable from the rest of the text as follows.

 $  echo ${NAME}7  Mike Ron7 $ 

Many UNIX users put {} around variable names to avoid any ambiguity. The curly brackets must be used any place a shell variable is used with some other text.

Modifying a Variable

Assigning a new value to the same variable name modifies the previous value of the variable. It can be done in two ways. If we just assign a new value, the old value of the variable is destroyed . We can also append to the old value by putting the variable name on the right-hand side of the = symbol at the time of assignment. For example, if we want to add a third part to the NAME variable, it can be done as follows.

 $  NAME="$NAME Junior"  $ $  echo $NAME  Mike Ron Junior $ 

Note

This is a very useful way to add your own directories to the PATH variable. The PATH variable set by the system administrator contains a list of directories where command files are located. When finding a command, if you want the shell to also search in your own directories, you can use the above method to append your own directory names to the PATH variable.


Single- and Double-Quote Characters

Now we come to the difference between single and double quotes. Consider the above command example by replacing the double quotes with single quotes and watch the result carefully .

 $  NAME='$NAME Junior'  $ $  echo $NAME  $NAME Junior $ 

This is not what we wanted! What happens is that single-quote characters do not expand any variable name inside them to its value. Instead, anything inside the single quotes is taken as is and assigned to the variable. One must be careful when using single quotes! The same rule applies when you use single and double quotes with other commands. See the results of two echo commands.

 $  NAME= "Mike Ron"  $ $  echo "$NAME Junior"  Mike Ron Junior $  echo '$NAME Junior'  $NAME Junior $ 

Removing a Variable

A shell variable can be removed by the unset command on HP-UX. Please note that this command is not available in all UNIX shells .

 $  NAME="Mike Ron"  $  echo $NAME  Mike Ron $  unset NAME  $  echo $NAME  sh: NAME: Parameter not set. $ 

Assigning Output of a Command to a Variable

On most keyboards, the back quote character is displayed when you press the " ~ " key without the SHIFT key. It is used to assign the result of a command to a variable. If you want to assign your login name to a variable NAME , you can use the following command.

 $  NAME=`whoami`  $ $  echo $NAME  boota $ 

You can also use the back quote character anywhere that you want to substitute the result of a command. In the following example, it is used with echo command.

 $  echo "My login name is `whoami`"  My login name is boota $ 

   
Top


HP Certified
HP Certified: HP-UX System Administration
ISBN: 0130183741
EAN: 2147483647
Year: 2000
Pages: 390
Authors: Rafeeq Rehman

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