-->
Previous Table of Contents Next


The PATH Variable

The PATH variable lists the directories in which the shell searches for commands. The shell searches those directories in the order they’re listed. If PATH=/bin:/usr/bin/:., whenever the shell interprets a command, it first looks in the directory /bin. If it can’t find the command there, the shell looks in the directory /usr/bin. Finally, the shell searches the . directory (remember that the dot represents your current directory). When you enter cal to print this month’s calendar, the shell first looks in /bin. Because the command isn’t there, the shell then looks in /usr/bin and finds it.


TIP:  If you have a personalized command named cal, the shell never finds it; the shell executes the cal command in /usr/bin first whenever you give the command. Give your commands names that aren’t the same as system commands.

You may want to put all your shell scripts in one directory and change the PATH variable to include that directory. This arrangement allows you to execute your shell scripts from whatever directory you happen to be in. To do this, follow these steps:

1.  Create a directory to hold the scripts. Use the mkdir $HOME/bin command to create the bin subdirectory in your home directory.
2.  Move each shell script to that subdirectory. For example, to move a shell script named stamp to your bin subdirectory, use the mv stamp $HOME/bin command.
3.  Add the script subdirectory to your PATH variable with the PATH=$PATH:$HOME/bin command. Do this in your .profile file so that the change takes effect every time you log in to your system.

You need to create that new bin directory and modify the PATH variable only once. Under Linux, the directory called /usr/local/bin is created to hold “local” commands and scripts that aren’t part of the standard Linux package but that you’ve added locally and have made available to all users. In this case, you should expect that /usr/local/bin is also part of PATH.

The MAIL Variable

The MAIL variable contains the name of the file that holds your e-mail. Whenever mail comes into the system for you, it’s put into the file specified by the MAIL variable. If you have a program that notifies you when new mail has arrived, it checks the file associated with the MAIL variable.

The PS1 Variable

The PS1 variable holds the string of characters you see as your primary prompt. The prompt is the string of characters the shell displays whenever it’s ready to receive a command. You see how you can change this variable—and any of the others—in the section “Customizing Linux Shells” near the end of this chapter.

The TERM Variable

The TERM variable is used to identify your terminal type. Programs that operate in full-screen mode, such as the vi text editor, need this information.

The TZ Variable

The TZ variable holds a string that identifies your time zone. The date program and some other programs require this information.

Your computer system keeps track of time according to Greenwich Mean Time (GMT). If the TZ variable is set to PST8PDT, the time and date are determined as Pacific Standard Time (PST), eight hours west of GMT, with support for Pacific Daylight Savings Time (PDT). Your computer system automatically changes between daylight savings time and standard time.

The LOGNAME Variable

The LOGNAME variable holds your login name, the name or string of characters that the system associates you with. Among the things the LOGNAME variable is used for is to identify you as the owner of your files, as the originator of any processes or programs you may be running, and as the author of mail or messages sent by the write command.

The following example is an extension of safrm, a shell script created for the safe removal of files. The LOGNAME variable is used to remove all the files you own from the directory /tmp. To do that, the shell script uses the find command. The find command has a number of options; the shell script uses this find command line:


find /tmp -user $LOGNAME -exec rm {} \;

The first parameter, /tmp, is the directory to search. The option -user indicates that you want to search for all files that belong to a specified user. Before the command is executed, the shell replaces $LOGNAME with the current user’s login name. The option -exec indicates that the following command is to be applied to every file found by the find program. In this case, the rm program is used to remove the found files. The braces ({}) represent the position of each filename passed to the rm command. The last two characters, \;, are required by the find command (an example of using the backslash to pass a character on to a program without being interpreted by the shell). Add this command line to the shell script in Listing 18.1 to obtain a program that removes files safely and also cleans up anything a user has in the /tmp directory that’s more than 10 days old.

Listing 18.1 The safrm Shell Script


# Name:     safrm

# Purpose:  copy files to directory /tmp, remove them

#             from the current directory, clean up /tmp,

#            and finally send mail to user

# first copy all parameters to /tmp

cp $* /tmp

# remove the files

rm $*

# create a file to hold the mail message

#    The file’s name is set to msg

#    followed by process ID number of this process

#    For example, msg1208

msgfile=/tmp/msg$$

# construct mail message

date > $msgfile

echo “These files were deleted from /tmp” >>$msgfile

# get list of files to be deleted from tmp

# -mtime +10 gets all files that haven’t been

# modified in 10 or more days, -print displays the names.

find /tmp -user $LOGNAME -mtime +10 -print >> $msgfile

# remove the appropriate files from /tmp

find /tmp -user $LOGNAME -mtime +10 -exec rm {} \;

# mail off the message

mail $LOGNAME < $msgfile

# clean up

rm $msgfile


Previous Table of Contents Next