Previous | Table of Contents | Next |
To create a Bourne or Korn shell variable, you simply assign the value of the variable to the name. If the value contains spaces or characters that the shell interprets in a special way, you must enclose the value in quotes. Refer to the section "Quoting" on page 337 for more information. Use the following syntax to assign the value of the variable to a name:
variable=value
In the following Bourne or Korn shell example, the variable today is set to display the output of the date command:
$ today=Tuesday $ echo $today Tuesday $
To display the value for a variable for any shell, type echo $variable. Although the Korn shell recognizes the echo command, print $variable is the preferred syntax. Optionally, you can enclose the name of the variable in curly braces ({}). You may want to use curly braces if you are concatenating strings together and want to separate the name of the variable from the information that follows it. In the following example, a variable named flower is set to rose. If you want to add an "s" at the end of the variable name when appears on-screen, you must enclose the variable in curly braces.
$ flower=rose $ echo $flower rose $ echo $flowers $ echo ${flower}s roses $
You can set local variables from the command line, as shown in the previous examples, or within a script.
Use the following syntax to set a C shell variable. If the value contains spaces or characters that the shell interprets in a special way, you must enclose the value in quotes. Refer to the section "Quoting" on page 337 for more information.
set variable=value
You can also set the value of a variable to return the output of a command. To do so, enclose the name of the command in backquotes (` `). The Korn shell also supports the notation $(command). The following C shell example sets the variable today to display the output of the date command:
oak% set today = `date` oak% echo $today Thu Jul 8 12:41:27 PDT 1993 oak%
Refer to the section "Quoting" on page 337 for more information.
You can use the unset command to remove any shell variable, as shown in the following Bourne shell example:
$ unset today $ echo $today $
Sometimes you want to modify a pathname to strip off unneeded parts. With the Bourne shell, you can use the basename(1) utility to return only the filename, and the dirname(1) utility to return only the directory prefix. The Korn and C shells provide a built-in way for you to modify path names.
The Korn shell provides pattern-matching operators, shown in Table 16-3 , that you can use to strip off components of pathnames.
Operator | Description |
---|---|
${variable#pattern} | Deletes the shortest part at the beginning of the variable that matches the pattern and returns the rest. |
${variable##pattern} | Deletes the longest part at the beginning of the variable that matches the pattern and returns the rest. |
${variable%pattern} | Deletes the shortest part at the end of the variable that matches the pattern and returns the rest. |
${variable%%pattern} | Deletes the longest part at the end of the variable that matches the pattern and returns the rest. |
The following example shows how all of the operators work, using the pattern /*/ to match anything between two slashes, and .* to match a dot followed by anything:
$ pathname=/home/winsor/Design.book.new $ echo ${pathname#/*/} winsor/Design.book.new $ echo ${pathname##/*/} Design.book.new $ echo ${pathname%.*} /home/winsor/Design.book $ echo ${pathname%%.*} /home/winsor/Design $
The C shell provides a set of modifiers that you can use to strip off unneeded components. These modifiers are quite useful in stripping pathnames, but can also be used to modify variable strings. Table 16-4 lists the C shell variable modifiers.
Modifier | Description |
---|---|
:e | Extensionremove prefix ending with a dot. |
:h | Headremove trailing pathname components. |
:r | Rootremove trailing suffixes beginning with a dot. |
:t | Tailremove all leading pathname components. |
:q | Quoteforce variable to be quoted. (Used to quote $argv.) |
:x | Like q, but break into words at each space, tab, or newline. |
The following example shows the results of the first four variable modifiers:
oak% set pathname = /home/winsor/Design.book oak% echo $pathname:e book oak% echo $pathname:h /home/winsor oak% echo $pathname:r /home/winsor/Design oak% echo $pathname:t Design.book
Previous | Table of Contents | Next |