Using Shell Variables and Quoting


Before you begin to create scripts, let's look at two concepts that will become increasingly important as you deal with the complexity of the tasks at hand. These concepts are shell variables (and variable substitution) and quoting.

Variables are special words that, when expanded, are replaced by values that you've assigned to them. The shell provides a built-in process for creating and keeping track of variables, known as variable substitution. Variable substitution enables you to generalize commands that are included in shell scripts, making it possible for you to change the way a script acts (for example, the files it will affect) simply by changing the values of the variables it uses.

The use of variables in shell scripts involves two basic steps:

1.

The creation of a variable, usually by simply assigning a value to a word using the equal sign.

Usually, this also involves using quotation marks to enclose the assigned value, to ensure that it is not misinterpreted by the shell.

2.

Later in your script, indicating to the shell that you want the value of the variable to be substituted for the variable's name exactly where it appears. You do this by prefixing the name of your variable with a dollar sign ($).

For example, through the use of variables, you can write a script that performs a specific set of operations on a file without having to explicitly name the file in the script, thus allowing the user to name the file that should be affected each time the script is run.

Creating and Substituting Variables

To understand how to create variables, assume that in your work on a day-to-day basis, you often encounter people stopping by your desk to request printouts of the documents named newempl.txt, hours.txt, and phonebook.txt, all of which are stored in /home/you/textfiles. The command you know to print these files from any present working directory is

[View full width]

[you@workstation20 you]$ lpr /home/you/textfiles/newempl.txt /home/you/textfiles/hours.txt /home/you/textfiles/phonebook.txt [you@workstation20 you]$

One way to reduce the amount of typing you have to do is to use a variable. You can create a variable to hold the names and paths of the files you need to repeatedly print, saving yourself the trouble of typing all the names and path information:

[View full width]

[you@workstation20 you]$ PRINTFILES="/home/you/textfiles/newempl.txt /home/you/textfiles /hours.txt /home/you/textfiles/phonebook.txt" [you@workstation20 you]$

You have now created a variable called PRINTFILES, which holds the list of filenames you so often need to print. To use the variable, substitute it for the file and pathname information in a command line. From now on, whenever you want to print these three files from this shell session, you can simply type

 [you@workstation20 you]$ lpr $PRINTFILES [you@workstation20 you]$ 

This command is much shorter! But how does it work? The shell replaces the word PRINTFILES with the contents of the variable PRINTFILES. To see this effect with a command that makes it more clearly visible, try using the echo command to display the value of PRINTFILES:

[View full width]

[you@workstation20 you]$ echo $PRINTFILES /home/you/textfiles/newempl.txt /home/you/textfiles/hours.txt /home/you/textfiles /phonebook.txt [you@workstation20 you]$

Because the shell replaced PRINTFILES with the value you assigned to it before calling the echo command, echo outputs the value you assigned to PRINTFILES.

After you create a variable, you can substitute the value of the variable anywhere at the command line or in a script by preceding the name of the variable with a dollar sign ($).

Be Specific When Using Variable Substitution

When variable names are ambiguous, you can also enclose the name of a variable in braces ({}) to indicate to the shell exactly where the variable name begins and ends.

For example, assume that you want to use the contents of the variable WORD immediately before the number 2000 in a script. The shell might mistakenly assume that you are referring to a variable called WORD2000:

 [you@workstation20 you]$ WORD=YEAR [you@workstation20 you]$ echo $WORD2000 [you@workstation20 you]$ echo ${WORD}2000 YEAR2000 [you@workstation20 you]$ 

By enclosing the name of your variable, WORD, in braces, you show the shell which variable name should be expandedWORD, the variable you created, rather than a variable called WORD2000 that you did not intend to create.


Quoting Carefully

When you create variables, it is usually a good idea to enclose the value you want to assign in quotation marks. They tell the shell that you want to assign exactly the value inside the quotes, rather than expand patterns or other variables or treat values separated by spaces as arguments or commands. Several types of quoting can be done at the command line; variables can be created or assigned using any of them:

 VARIABLE=value VARIABLE="value" VARIABLE='value' 

Each of these commands is subtly different. The first method (no quoting) is the simplest. However, because the shell normally uses a number of characters in special ways, such as the space to separate arguments or the vertical bar (|) to create a pipe, quoting is necessary if you want to assign these characters to a variable:

 [you@workstation20 you]$ MYNAME=Horace Walpole bash: Walpole: command not found [you@workstation20 you]$ MYNAME="Horace Walpole" [you@workstation20 you]$ 

In this case, quoting is needed to prevent the shell from using the space as a separation value. Among the other characters that must be enclosed in quotation marks if assigned to variables are parentheses (()), braces ({}), and brackets ([]). There is also a difference between the result of enclosing data in single quotation marks and the result of enclosing data in double quotation marks. In double quotation marks, although most special characters are ignored, command and variable substitution are still performed; in single quotation marks, they are not. This difference is subtle and perhaps best illustrated by example. Consider the following sequence:

 [you@workstation20 you]$ QUOTEA="My name is $MYNAME." [you@workstation20 you]$ QUOTEB='My name is $MYNAME.' [you@workstation20 you]$ echo $QUOTEA My name is Horace Walpole. [you@workstation20 you]$ echo $QUOTEB My name is $MYNAME. [you@workstation20 you]$ 

As you can see here, when a phrase is placed within single quotation marks, the dollar sign ($) ceases to be a special character. When a phrase is placed within double quotation marks, the dollar sign retains its special meaning and variables are substituted as necessary, although other special characters lose their unique functionality. This example using the date command and command substitution simply prints the current date to standard output:

 [you@workstation20 you]$ date Thu Aug 26 14:34:02 MDT 2004 [you@workstation20 you]$ TIMEA="The time right now is $(date)." [you@workstation20 you]$ TIMEB='The time right now is $(date).' [you@workstation20 you]$ echo $TIMEA The time right now is Thu Aug 26 14:34:14 MDT 2004. [you@workstation20 you]$ echo $TIMEB The time right now is $(date). [you@workstation20 you]$ 

Because quoting can so radically affect the behavior of shell variable substitution and shell command substitution, you need to gain an understanding of the way it works. Feel free to experiment a little longer with variables and quoting until you feel comfortable using them.

Understanding Environment Variables

Before we finish looking at variables and quoting, it is important to learn about a special class of variables known as environment variables. These variables tell the shell how to behave as you work at the command line or in scripts; changing the value of these variables changes some behavioral aspect of the shell.

The single most important environment variable is PATH. The PATH variable contains a list of all the directories that are known to hold commands. When you type a command at the command line, the shell searches through all the directories listed in PATH until it finds a program by that name. The shell loads and runs the program (in reality, a command) and passes on to it any arguments you have supplied. Try printing the contents of PATH now:

 [you@workstation20 you]$ echo $PATH /usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/you/bin [you@workstation20 you]$ 

In this case, the shell searches for commands in /usr/local/bin, /bin, /usr/bin, /usr/X11R6/bin, and /home/you/bin, in that order. To see where the shell eventually finds any given command, you can use the which command:

 [you@workstation20 you]$ which emacs /usr/bin/emacs [you@workstation20 you]$ 

This means that when you type emacs at the command line, the shell uses the PATH variable to search in /usr/local/bin and /bin before finally finding emacs in /usr/bin. If you add to the PATH variable, you can cause the shell to search in more places:

 [you@workstation20 you]$ PATH="/usr/sbin:$PATH" [you@workstation20 you]$ echo $PATH /usr/sbin:/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/you/bin [you@workstation20 you]$ 

You have now added an additional directory, /usr/sbin, to the PATH; /usr/sbin will be searched first for the remainder of this shell session whenever a command is entered.

Perhaps the most important thing to notice about the default value of PATH in Linux is the fact that /home/you/bin appears in it. This means that as you create shell scripts throughout the rest of this chapter, you will be able to place them in the bin directory in your home directory and then use them like any other command.

For more information on the available environment variables and their functions, see the manual page for bash.



    SAMS Teach Yourself Red Hat(r) Fedora(tm) 4 Linux(r) All in One
    Cisco ASA and PIX Firewall Handbook
    ISBN: N/A
    EAN: 2147483647
    Year: 2006
    Pages: 311
    Authors: David Hucaby

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