Shell Variables


The shell provides a mechanism to define variables that can be used to hold pieces of information. Shell variables can be used to customize the way in which programs (including the shell itself) interact with you. This section will describe some of the standard variables used by the shell and other programs, and explain what they do for you.

Table 4–3 summarizes the commands for assigning variables and aliases in the various shells.

Table 4–3: Assigning Variables and Aliases

sh, ksh, or bash

csh or tcsh

Effect

VAR=value

set var=value

Assign a value to a variable

$VAR

$var

Get the value of a variable

set

set

List shell variables

unset VAR

unset var

Remove a variable

env

env

List all environment variables

VAR=value; export VAR export VAR=value

setenv var value

Create an environment variable

unset VAR

unsetenv var

Remove an environment variable

set -o

 

View shell options

set -o option

set option

Turn on a shell option

set +o option

unset option

Turn off an option

alias name=value

alias name value

Create a command alias

unalias name

unalias name

Remove an alias

Variables in sh, ksh, and bash

You define a shell variable by typing a name followed by an=sign and a value. For example, you could create a variable called PROJDIR to save the pathname for a directory you use often:

 $ PROJDIR=/home/nate/Work/cs106x/Proj_3/lib/Source

To assign a value with a space in it, use quotes, like this:

 $ FILELIST='graphics.c strings.c sorting.c'

In the Bourne-compatible shells, variable names are conventionally written in uppercase letters, although you can use lowercase names as well. As with filenames, variable names are case-sensitive, so the shell will treat PROJDIR and projdir as two different variables.

To get the value of a shell variable, precede the variable name with a dollar sign, $. You can print a variable with the echo command, which copies its standard input to its standard output.

 $ echo $PROJDIR /home/nate/Work/cs106x/Proj ect_3/lib/Source

You can also use variables in command lines. When the shell reads a command line, it interprets any word that begins with $ as a variable, and replaces that word with the value of the variable. For example,

 $ cp $PROJDIR/graphics.c .

will copy the file /home/nate/Work/cs106x/Project_3/lib/Source/gmphics.c to the current directory

You can use the set command to view all of your current shell variables and their values. A typical output from set might look like

 $ set COLUMNS=80 HOME=/home/raf HOSTNAME=localhost.localdomain MAIL=/var/spool/mail/raf MAILCHECK=30 PATH=/usr/local/bin:/bin:/usr/bin:/home/raf/bin PROJDIR=/home/nate/Work/cs106x/Project_3/lib/Source PS1='$ ' PS2='> ' SHELL=/bin/bash TERM=vt100

Most of the variables on this list are standard shell variables that will be discussed later in this section. The exception is PROJDIR, which is a user-defined variable with no special meaning.

To remove a variable, use the command unset, as in

 $ unset PROJDIR $ echo $PROJDIR $

Environment Variables in sh, ksh, and bash

When you run a command, the shell makes certain shell variables and their values available to the program. The program can then use this information to customize its actions. The collection of variables and values provided to programs is called the environment.

Your environment includes variables set by the system, such as HOME, LOGNAME, and PATH (described in the next section). You can display your environment variables with the command env:

 $ env HOSTNAME=localhost.localdomain SHELL=/bin/bash MAIL=/var/spool/mail/raf PATH=/usr/local/bin:/bin:/usr/bin:/home/raf/bin PWD=/home/raf/Proj ect PS1='$ ' HOME=/home/raf LOGNAME=raf

To make variables that you define yourself available to commands as part of the environment, they must be exported. For example, TERM is a common shell variable that is not always automatically part of the environment. To make it available to commands, you first define, then export it:

 $ TERM=vt100 $ export TERM

A shortcut that does the same thing in ksh or bash is

 $ export TERM=vt100

Common Shell Variables in sh, ksh, and bash

The following is a short summary of some of the most common shell variables, including those set automatically by the system.

  • HOME contains the absolute pathname of your login directory HOME is automatically defined and set to your login directory as part of the login process. The shell itself uses this information to determine the directory to change to when you type cd with no argument.

  • LOGNAME contains your login name. It is set automatically by the system.

  • PWD is a special variable that gets set automatically to your present working directory You can use this variable to include your current directory in the prompt or in a command line.

  • PATH lists the directories in which the shell searches to find the program to run when you type a command. A default PATH is set by the system, but many users modify it to add additional command directories.

A typical example of a customized PATH, in this case for user anita, is the following:

 PATH=$PATH:/sbin:/usr/bin:/home/anita/bin

This setting for PATH indicates that when you enter a command, the shell first searches for the program in the default path (the previous value of the PATH variable), then in the directory /sbin; then in /usr/bin; and finally in the bin subdirectory of the user’s login directory. In these pathnames, bin stands for binaries, meaning executable programs. The directories /bin, /usr/bin, /sbin, and /usr/sbin are common locations for important commands and programs. If these directories are not in your default path, you may want to add them.

It is also common to create a subdirectory called bin in your home directory Instead of adding every directory that contains a command or executable to your PATH, you can create symbolic links to the commands in your bin directory (Remember to give yourself execute permission for the symbolic links. Creating links, and adjusting the permissions on them, is discussed in Chapter 3.)

  • CDPATH is similar to PATH. It lists, in order, the directories in which the shell searches to find a subdirectory to change to when you use the cd command. This means that you can “jump” from one directory to another without typing the full pathname. A good choice of CDPATH can make it much easier to move around in your file system.

  • ENV is a very important variable in the Korn shell. It tells ksh where to find the environment file that it reads at startup. If you are using ksh, the ENV variable should be set in your .profile. A common value is $HOME/.kshrc.

  • PS1 defines your prompt. The default value is $. Similarly, PS2 defines your secondary prompt, and has a default value of >. Most users like to customize the prompt by adding information such as the current working directory For example,

     $ PS1='$LOGNAME $PWD> ' saul /home/saul/Email>

  • TMOUT tells the shell how many seconds to wait before timing out. If you don’t type a command within that period of time, the shell logs you off. This variable is not supported by sh; you can define it, but it won’t do anything. By default, TMOUT is set to 0, meaning that it will never time out.

  • MAIL contains the name of the file in which your newly arriving mail is placed. The shell uses this variable to notify you when new information is added to this file. This variable is set automatically when you log in.

  • MAILCHECK tells the system how frequently, in seconds, to check for new mail. By default, this is set to 60 in bash, 600 in ksh.

  • HISTSIZE tells the shell how many commands to save in your history file (see the section “Command History” later in this chapter). Not supported by sh. The default value for HISTSIZE in bash is 500; in ksh it is 128.

  • HISTFILE (which is also not supported by sh) specifies the location of your history file, such as .history (ksh) or .bash_history (bash).

  • TERM is used by vi and other screen-oriented programs to get information about the type of terminal you are using. This information is necessary to allow the programs to match their output to your terminal’s capabilities, and to interpret your terminal’s input correctly A common value is “vt100”.

  • SHELL contains the name of your shell program. This is used by some interactive commands to determine which shell program to run when you issue a shell escape command. (A shell escape temporarily interrupts the program and runs a shell for you.) This variable is typically set automatically at login.

  • VISUAL is a variable used only by ksh. It can be used to determine which command-line editor the shell uses, although this can also be done with an option setting. See the section “Command-Line Editing,” later in this chapter.

Shell Options in ksh and bash

The Korn shell and bash provide a number of options that turn on special features. To turn on an option, use the set command with -o (option) followed by the option name. To view your current option settings, use set -o by itself.

The noclobber option prevents you from overwriting an existing file when you redirect output from a command. This can save you from losing data that may be difficult or impossible to replace. You can turn on noclobber with set:

 $ set -o noclobber

Suppose noclobber is set, and your current directory contains a file named temp. If you try to redirect the output of a command to temp, you get a warning:

 $ ls -1 > temp temp: file exists

You can tell the shell that you really do want to overwrite the file by putting a bar (pipe symbol) after the redirection symbol, like this:

 $ ls -1 >| temp

To turn off an option, use set +o, as in

 $ set +o noclobber

The ignoreeof feature prevents you from accidentally logging yourself off by typing CTRL-D. If you use this option, you must type exit to terminate the shell.

The notify option causes the shell to notify you as soon as your background jobs complete, without waiting for you to finish your current command. Some users find this useful, but others may not like getting interrupted by notifications.

You can also use set to turn on the screen editor option for command-line editing (discussed later in this chapter). The following line,

 $ set -o emacs

tells the shell that you want to use the emacs-style command-line editor. You could use vi instead. (The text editors vi and emacs are covered in Chapter 5.)

Variables in csh and tcsh

As with the Bourne-compatible shells, the C shell and tcsh provide variables, including both standard system-defined variables and ones you define yourself. However, there are a number of differences in the way variables are defined, what they are named, and how they are used.

In csh and tcsh, you define a variable with the set command, as shown here:

 % set projdir = /home/nate/Work/cs106x/Proj_3/lib/Source

If the value has a space in it, you must put quotes around it, like this:

 % set filelist = 'graphics.c strings.c sorting.c'

C shell variables are generally lowercase. If you do create variables with names in uppercase, remember that variable names are case-sensitive, so the shell will treat filelist and FileList as two different variables.

To get the value of a shell variable, type a $ followed by the variable name. Since the echo command prints its standard input to its standard output, the command line echo $VARNAME will print the value of a variable:

 % echo $projdir /home/nate/Work/cs106x/Project_3/lib/Source

You can also use variables in command lines. For example,

 % cp $projdir/graphics.c .

will copy the file /home/nate/Work/cs106x/Project_3/lib/Source/graphics.c to the current directory

As with the other shells, you can use the set command to view all of your current shell variables and their values, and the unset command to undefine a variable.

 % unset projdir % echo $projdir projdir: Undefined variable. %

Environment Variables in csh and tcsh

There are certain variables that the shell makes available to commands as part of the environment that the shell maintains. Commands can use these variables to get information such as your login name or the size of your screen. These variables are called environment variables.

To set an environment variable, use the command setenv:

 % setenv term vt100

Note that, unlike defining a variable with set, you do not use an=sign when setting an environment variable with setenv.

You can view all of your environment variables with the command env. To remove a variable from the environment, use unsetenv.

Common Shell Variables in csh and tcsh

These are some of the most common C shell variables, including those set automatically by the system:

  • home is the full pathname of your login directory

  • user is your username.

  • cwd holds the full name of the directory you are currently in (the current working directory). It provides the information the pwd command uses to display your current directory.

  • path holds the list of directories the C shell searches to find a program when it executes your commands. It corresponds to the PATH variable in the Bourne-compatible shells.

By default, path is set to search first in your current directory, and then in /usr/bin. To add the directories /bin, /sbin, /usr/sbin, and your own bin directory to path, put a line like this in your .cshrc or .tcshrc file:

 path = ($path /bin /sbin /usr/sbin $home/bin)

Because these directories are all common locations for commands and programs, you may want to add them to your path if they are not there already The directory $home/bin is often used to hold symbolic links to other commands. This way, you can run the commands without having to add a long list of directories to your path.

Unlike ksh or bash, which use a colon to separate items in the path, the C shell uses parentheses to group the different directories included in path. This use of parentheses to group multivalued variables is a general feature of the C shell. Other standard C shell variables with multiple values include cdpath and mail.

  • cdpath is the C shell equivalent of the CDPATH variable. It lists in order the directories in which csh or tcsh searches when you use the cd command. This allows you to move from one directory to another without typing the full pathname.

  • The prompt variable allows you to customize the prompt. You can set it to include information such as your username or working directory For example, you can set the tcsh prompt like this:

     % set prompt="[%n@%m %c] tcsh % " [liz@localhost ~/Email] tcsh %

    The default C shell prompt is %. Note that unlike sh, the C shell does not allow you to redefine the secondary prompt.

  • The variable mail tells the shell how often to check for new mail, and where to look for it.

     % set mail = ( 60 /var/spool/mail/liz )

    This setting causes csh to check the file /var/spool/mail/liz every 60 seconds. If new mail has arrived in the directory specified in mail since the last time it checked, csh displays the message, “You have new mail.”

  • history is the number of commands the shell saves in your history file.

  • histfile is the name of the history file.

  • term identifies your terminal type. A common value is vt100.

Shell Options in csh and tcsh

The C shell uses special variables called toggles to turn certain shell features on or off. Toggle variables are variables that have only two settings: on and off. When you set a toggle variable, you turn the corresponding feature on. To turn it off, you use unset. Important toggle variables include noclobber, ignoreeof, and notify.

The noclobber toggle prevents you from overwriting an existing file when you redirect output from a command. To turn on the noclobber feature, use set as shown in this example:

 % set noclobber

Suppose noclobber is set, and that a file named temp already exists in your current directory If you try to redirect the output of a command to temp, you get a warning like this:

 % ls -1 > temp temp: file exists

The preceding example tells you that a file named temp already exists and that your command will overwrite it. You can tell the shell that you really do want to overwrite a file by putting an exclamation mark after the redirection symbol:

 % ls -1 >! temp

The ignoreeof toggle prevents you from accidentally logging yourself off by typing CTRL-D. This is a good command to add to your .cshrc or .tcshrc file:

 % set ignoreeof

The notify toggle informs you when a background job finishes running. If notify is set, the shell will display a message when a background job is complete. This toggle is set by default, but if you do not want to get job completion messages while you are in the middle of something else, you can unset it, as shown here:

 % unset notify




UNIX. The Complete Reference
UNIX: The Complete Reference, Second Edition (Complete Reference Series)
ISBN: 0072263369
EAN: 2147483647
Year: 2006
Pages: 316

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