Professional Documents
Culture Documents
29
Chapter Syllabus 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.
Environment Variables
30
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 $
Environment Variables As you can see, the echo command has displayed its argument text, not the variable value.
31
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.
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 $
32
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.
Environment Variables
33
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. $
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 $
Environment Variables
34
HOME is automatically set when a user logs into HP-UX. It contains the path of the user's home directory. To refer to the .profile file in your home directory, you can use $HOME/.profile as the complete path to the file. Please note that there is another way to refer to your home directory in HP-UX, and that is the ~/ combination. PWD shows the current directory. It is also set automatically whenever you use the cd command. It always has the value of the current directory. SHELL shows the absolute path of your login shell. It is automatically set at the login time. TERM contains the name or type of your terminal. It is usually set through the /etc/profile shell startup file using the tset or ttytype command. PS1 contains the primary command prompt string. This string is displayed in your shell prompt. If $ is displayed as your command prompt, the value of PS1 is a $ symbol. PS2 contains the secondary command prompt string. That is, if you issue a command that is incomplete, you will see a prompt by the shell with a value of PS2. MANPATH contains the list of directories where the man command looks for manual pages. A colon separates each directory in the list. TZ contains the local time zone that was set at the time of HP-UX installation. Commands such as date read this variable. EDITOR contains the name of the editor you want to use for command line editing or for typing mail messages. You can set this variable in your shell startup file with your favorite editor. HISTFILE contains the name of the history file where your command history is kept. All of the commands you use go to the history file. HISTSIZE variable shows how many commands are kept in the history file.
Study Break
Predefined Environment Variables
Knowledge of shell variables is very helpful in understanding the behavior of some commands. Use the command:
echo "Incomplete command test
You will see a > symbol in the next line. You get back neither the command prompt nor any other display. You may be wondering what happened to the command. Actually, you issued an incomplete command, missing the closing double quotes. The symbol > is the value of the PS2 environment variable that is displayed whenever you use an incomplete command. Just complete the remaining part of the command and press the key at this prompt.
Change this variable so that it gives a more meaningful message, such as "Incomplete command>" and again use the same command to check if it works. Using the set command, see which variables are set on your system. To find out more about an environment variable, get help with the man sh-posix command.
Environment Variables
35
In the next example, I demonstrate the difference between exported and nonexported variables. First we create a variable NAME and then start a child shell by executing the sh command. When we use the echo command in the child shell for the NAME variable, it is empty. Now we use the exit command to return to the parent shell and export the NAME variable. We start the child shell again, and now the variable is visible in the child shell. Finally, we use exit again to return to the parent shell.
$ NAME="Mike Ron" $ echo $NAME Mike Ron $ sh $ echo $NAME $ $ exit $ export NAME $ sh $ echo $NAME Mike Ron $ exit $
Note
If you make a change to an environment variable, always export it. This is necessary so that the correct value of the variable is inherited by the child processes.
As you notice, the command prompt changes as soon as you change the value of PS1. Just to remind you, you also have to export the PS1 variable to make this change visible in the sub-shells.
As you can see, the prompt changes and the current directory is there. Now, whenever you use the cd command to change the current directory, the PWD environment variable changes, and your prompt also changes to show the new current directory. Similarly, it is possible to have your login name and the computer name in the command prompt along with the current directory, as shown in this example.
$ PS1= "`whoami` on `uname -n` \$PWD =>" boota on myhp /home/boota =>
Environment Variables
Note
36
This is very useful if you work in a network environment and often log into different machines with different names. It is the command prompt that tells you which machine you are on, which login name you are using, and what the current directory is. Does this seem interesting to you? You can also try to change your prompt to include the date and time, blinking characters, and so on, depending on the type of terminal you are using.
If you have placed your own commands in your home directory, you can add your home directory to the PATH variable with the following command.
$ PATH=$PATH:$HOME $
Although you are free to add the HOME directory at the beginning or end of the PATH variable, it is always desirable to add your own directory names at the end. This is because, if you accidentally use a program or script name, the same as a standard HP-UX command, your program will be executed instead of the HP-UX command, as it will be found first by the shell.
Chapter Summary
In this chapter, you learned the difference between environment and shell variables, how to set and change the values of variables, and how to utilize environment variables for some useful tasks. You have seen how to avoid ambiguities when using variables with the help of curly brackets. You also have knowledge of important predefined variables and their uses. You have learned other useful information, such as the difference between single and double quotes and command substitution using back quotation characters. Some useful information was also added to the command prompt with the use of PS1 variable. In the next chapter, you will see the use of I/O redirection and pipes, where you will be able to use the output of one command as the input to another command.
37
D. none of the above 2: What is not true about the PATH variable? A. B. C. D. It shows the path of the current directory. It shows the names of directories that are checked when searching for a command. It is usually set in the /etc/profile by the system administrator. The value of the PATH variable can be changed by a user.
3: You have a variable ABC with value Alphabets. You use the following command to change its value.
ABC='All $ABC'
D. back-quote characters 5: The value of the PS2 variable on your system is > (greater-than symbol). You issue an incomplete command. What will be the shell's response? A. B. C. D. A. B. C. D. The shell will issue an error message showing that the command is incomplete. The shell will display PS2. The shell will try to execute that part of the command that is supplied. The shell will give a > prompt and wait for you to complete the command. It is not a legal name, because a shell variable must contain only uppercase letters. It is not legal, because variables can only have uppercase or lowercase letters, not a combination. It starts with a digit, so it is not a legal name. It ends with a digit, so it is not a legal name.