• No results found

Environment and Shell Variables

In document Hp-Ux Unix Rehman (Page 39-44)

Test Your Knowledge

Chapter 3. Environment Variables

3.1 Environment and Shell Variables

3.2 Setting and Displaying Variables 3.3 Predefined Environment Variables 3.4 Exporting Shell Variables

3.5 Setting the Command Prompt 3.6 The PATH Variable

As soon as a user logs into HP-UX, the shell is invoked and waits for commands from the user.

To execute these commands, the shell needs to know some information about the environment being used. For example, to correctly display a file, the shell needs to know which type of terminal is attached to the system. Similarly, when a user issues an extrinsic command, the shell needs to know in which directories it should look for the command. In UNIX terminology, we call this type of information the shell environment.

The shell stores environment information in environment variables. Usually, many types of variables are set in the system startup file (/etc/profile) managed by the system administrator.

The users can also set environment variables through the use of a user startup file kept in the home directory (.profile).

Any program that needs environment variables checks the existence and value of these variables at the startup time. For example, the editor program vi needs your terminal information to correctly display and scroll text. When you start the vi editor, it will check the TERM variable. If it understands the terminal type set by the TERM variable, it will start in full screen mode; otherwise, it will start in line editing mode, where you can edit or display only one line of text at a time. Similarly, the more command needs the TERM variable to display a particular number of text lines, depending on the type of terminal being used.

You can modify and change environment variables set by the system administrator. The system administrator usually sets the PATH variable that shows the search path for the executable commands. But as you start using the UNIX system, you also create your own programs and scripts, and you want the shell to look into the directories containing your own programs as well. For this purpose, you can add your own directory names in the PATH variable.

In this chapter, you will see the difference between environment and shell variables and how to set and display variables. There are many predefined environment variables, and the most important of these will be discussed. Then you will learn how to increase the visibility of a shell variable by exporting it. The default HP-UX command prompt shows little information, and you will see how to add some useful information to it using variables. Since PATH is an important variable, you will learn more about it at the end of the chapter.

3.1 Environment and Shell Variables

When a shell executes a command, UNIX creates a process in memory for that command.

This process is called the child process of the shell. Because the shell created this process, the shell is called the parent process of the command process.

You can also invoke a shell within another shell. The newly created shell is the child shell. You will be writing shell scripts when you move to the fourth section of the book. These shell scripts are executed within the parent shell as child processes.

All child processes inherit environment variables from their parent (the shell). On the other hand, shell variables are set locally by the shell and are not visible to any child process. Each child gets a copy of the environment variables and is allowed to make changes to these variables. But it should be kept in mind that these changes are local to the child process and can't reflect back. This means that changes made to the environment variables are lost as soon as the child process finishes. Or you can say that a child process can't make changes to the parent variables. The differences between shell and environment variables are presented in Table 3-1.

Table 3-1. Comparison of Shell and Environment Variables

Environment Variables Shell Variables Also called global variables Also called local variables

Inherited by all child processes Not inherited by children Usually contain system-specific

information

Usually used to keep temporary values in shell programs

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:/usr/local/man/%L:/usr/local/m an

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:/usr/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

$

In document Hp-Ux Unix Rehman (Page 39-44)