Previous Section  < Day Day Up >  Next Section

13.10. Variables

13.10.1 Types of Variables

There are two types of variables: local and environment. Local variables are known only to the shell in which they were created. Environment variables are available to any child processes spawned from the shell from which they were created. Some variables are created by the user and others are special shell variables.

13.10.2 Naming Conventions

Variable names must begin with an alphabetic or underscore character. The remaining characters can be alphabetic, decimal digits (0 to 9), or an underscore character. Any other characters mark the termination of the variable name. Names are case sensitive. When assigning a value to a variable, do not include any whitespace surrounding the equal sign. To set the variable to null, follow the equal sign with a newline. The simplest format for creating a local variable is to assign a value to a variable in the following format.

FORMAT


variable=value


Example 13.49.

name=Tommy


13.10.3 The declare Built-In

There are two built-in commands, declare and typeset, used to create variables, with options to control the way the variable is set. The typeset command (from Korn shell) is exactly the same as the declare command (bash). The bash documentation says, "The typeset command is supplied for compatibility with the Korn shell; however, it has been deprecated in favor of the declare built-in command."[8] So from this point on we'll use the declare built-in (even though we could just as easily have chosen to use typeset).

[8] Bash Reference Manual: www.delorie.com/gnu/docs/bash/bashref_56.html.

Without arguments, declare lists all set variables. Normally read-only variables cannot be reassigned or unset. If read-only variables are created with declare, they cannot be unset, but they can be reassigned. Integer-type variables can also be assigned with declare.

FORMAT


declare variable=value


Example 13.50.

declare name=Tommy


Table 13.13. declare Options

Option

Meaning

–a[a]

Treats variable as an array (i.e., assigns elements)

–f

Lists functions names and definitions

–F[a]

Lists just function names

–i

Makes variables integer types

–r

Makes variables read-only

–x

Exports variable names to subshells


[a] –a and –F are implemented only on versions of bash 2.x

13.10.4 Local Variables and Scope

The scope of a variable refers to where the variable is visible within a program. For the shell, the scope of local variables is confined to the shell in which the variable is created.

When assigning a value, there can be no whitespace surrounding the equal sign. To set the variable to null, the equal sign is followed by a newline.[9]

[9] A variable set to a value or to null will be displayed by using the set command, but an unset variable will not.

A dollar sign is used in front of a variable to extract the value stored there.

The local function can be used to create local variables, but this is only used within functions. (See "Defining Functions" on page 841.)

Setting Local Variables

Local variables can be set by simply assigning a value to a variable name, or by using the declare built-in function as shown in Example 13.51.

Example 13.51.

1   $ round=world or declare round=world

    $ echo $round

    world

2   $ name="Peter Piper"

    $ echo $name

    Peter Piper

3   $ x=

    $ echo $x

4   $ file.bak="$HOME/junk"

    bash: file.bak=/home/jody/ellie/junk: not found


EXPLANATION

  1. The local variable round is assigned the value world. When the shell encounters the dollar sign preceding a variable name, it performs variable substitution. The value of the variable is displayed. (Don't confuse the prompt $ with the $ used to perform variable substitution.)

  2. The local variable name is assigned the value "Peter Piper". The quotes are needed to hide the whitespace so that the shell will not split the string into separate words when it parses the command line. The value of the variable is displayed.

  3. The local variable x is not assigned a value. It will be assigned null. The null value, an empty string, is displayed.

  4. The period in the variable name is illegal. The only characters allowed in a variable name are numbers, letters, and the underscore. The shell tries to execute the string as a command.

Example 13.52.

1   $ echo $$

    1313

2   $ round=world

    $ echo $round

    world

3   $ bash            # Start a subshell

4   $ echo $$

    1326

5   $ echo $round

6   $ exit           # Exits this shell, returns to parent shell

7   $ echo $$

    1313

8   $ echo $round

    world


EXPLANATION

  1. The value of the double dollar sign variable evaluates to the PID of the current shell. The PID of this shell is 1313.

  2. The local variable round is assigned the string value world, and the value of the variable is displayed.

  3. A new bash shell is started. This is called a subshell, or child shell.

  4. The PID of this shell is 1326. The parent shell's PID is 1313.

  5. The local variable round is not defined in this shell. A blank line is printed.

  6. The exit command terminates this shell and returns to the parent shell. (Ctrl-D will also exit this shell.)

  7. The parent shell returns. Its PID is displayed.

  8. The value of the variable round is displayed. It is local to this shell.

Setting Read-Only Variables

A read-only variable is a special variable that cannot be redefined or unset. If, however, the declare function is used, a read-only variable can be redefined, but not unset.

Example 13.53.

1   $ name=Tom

2   $ readonly name

    $ echo $name

    Tom



3   $ unset name

    bash: unset: name: cannot unset: readonly variable

4   $ name=Joe

    bash: name: readonly variable



5   $ declare -r city='Santa Clara'

6   $ unset city

    bash: unset: city: cannot unset: readonly variable



7   $ declare city='San Francisco'   # What happened here?

    $ echo $city

    San Francisco


EXPLANATION

  1. The local variable name is assigned the value Tom.

  2. The variable is made read-only.

  3. A read-only variable cannot be unset.

  4. A read-only variable cannot be redefined.

  5. The declare built-in command assigns a read-only variable, city, the value Santa Clara. Quotes are necessary when assigning a string containing whitespace.

  6. Because it is read-only, the variable cannot be unset.

  7. When a read-only variable is created with the declare command, it cannot be unset, but it can be reassigned.

13.10.5 Environment Variables

Environment variables are available to the shell in which they are created and any subshells or processes spawned from that shell. They are often called global variables to differentiate them from local variables. By convention, environment variables are capitalized. Environment variables are variables that have been exported with the export built-in command.

The shell in which a variable is created is called the parent shell. If a new shell is started from the parent shell, it is called the child shell. Environment variables are passed to any child process started from the shell where the environment variables were created. They are passed from parent to child to grandchild, and so on, but not in the other direction—a child process can create an environment variable, but cannot pass it back to its parent, only to its children.[10] Some of the environment variables, such as HOME, LOGNAME, PATH, and SHELL, are set before you log on by the /bin/login program. Normally, environment variables are defined and stored in the .bash_profile file in the user's home directory. See Table 13.14 for a list of environment variables.

[10] Like DNA, inheritance goes in one direction only, from parent to child.

Table 13.14. bash Environment Variables

Variable Name

Meaning

_ (underscore)

The last argument to the previous command.

BASH

Expands to the full pathname used to invoke this instance of bash.

BASH_ENV

Same as ENV but set only in bash.[a]

BASH_VERSINFO

Version information about this version of bash.[a]

BASH_VERSION

Expands to the version number of this instance of bash.

CDPATH

The search path for the cd command. This is a colon-separated list of directories in which the shell looks for destination directories specified by the cd command. A sample value is .:~:/usr.

COLUMNS

If set, defines the width of the edit window for shell edit modes and the select command.

DIRSTACK

The current contents of the directory stack of the bash.[a]

EDITOR

Pathname for a built-in editor: emacs, gmacs, or vi.

ENV

The environment file that is executed every time a new bash shell is started, including a script. Normally the filename assigned to this variable is .bashrc. The value of ENV is subjected to parameter expansion, command substitution, and arithmetic expansion before being interpreted as a pathname.

EUID

Expands to the effective user ID of the current user, initialized at shell startup.

FCEDIT

Default editor name for the fc command.

FIGNORE

A colon-separated list of suffixes to ignore when performing filename completion. A filename whose suffix matches one of the entries in FIGNORE is excluded from the list of matched filenames. A sample value is .o:~.

FORMAT

Used to format the output of the time reserved word on a command pipeline.

GLOBIGNORE

A list of files that will be ignored during filename expansion (called globbing).[a]

GROUPS

An array of groups to which the current user belongs.[a]

HISTCMD

The history number, or index in the history list, of the current command. If HISTCMD is unset, it loses its special properties, even if it is subsequently reset.

HISTCONTROL

If set to a value of ignorespace, lines that begin with a space character are not entered on the history list. If set to a value of ignoredups, lines matching the last history line are not entered. A value of ignoreboth combines the two options. If unset, or if set to any other value than those above, all lines read by the parser are saved on the history list.

HISTFILE

Specifies file in which to store command history. The default value is ~/.bash_history. If unset, the command history is not saved when an interactive shell exits.

HISTFILESIZE

The maximum number of lines contained in the history file. When this variable is assigned a value, the history file is truncated, if necessary, to contain no more than that number of lines. The default value is 500.

HISTSIZE

The number of commands to remember in the command history. The default value is 500.

HOME

Home directory; used by cd when no directory is specified.

HOSTFILE

Contains the name of a file in the same format as in /etc/hosts that should be read when the shell needs to complete a hostname. The file may be changed interactively; the next time hostname completion is attempted, bash adds the contents of the new file to the already existing database.

HOSTTYPE

Automatically set to the type of machine on which bash is executing. The default is system-dependent.

IFS

Internal field separators, normally SPACE, TAB, and NEWLINE, used for field splitting of words resulting from command substitution, lists in loop constructs, and reading input.

IGNOREEOF

Controls the action of the shell on receipt of an EOF character as the sole input. If set, the value is the number of consecutive EOF characters typed as the first characters on an input line before bash exits. If the variable exists but does not have a numeric value, or has no value, the default value is 10. If it does not exist, EOF signifies the end of input to the shell. This is only in effect for interactive shells.

INPUTRC

The filename for the readline startup file, overriding the default of ~./inputrc.

LANG

Used to determine the locale category for any category not specifically selected with a variable starting with LC_.[a]

LC_ALL

Overrides the value of LANG and any other LC_ variable.[a]

LC_COLLATE

Determines the collation order used when sorting the results of pathname expansion and the behavior of range expressions, equivalence classes, and collating sequences when matching pathnames and patterns.[a]

LC_MESSAGES

Determines the locale used to translate double-quoted strings preceded by a $.[a]

LINENO

Each time this parameter is referenced, the shell substitutes a decimal number representing the current sequential line number (starting with 1) within a script or function.

MACHTYPE

Contains a string describing the system on which bash is executing.[a]

MAIL

If this parameter is set to the name of a mail file and the MAILPATH parameter is not set, the shell informs the user of the arrival of mail in the specified file.

MAIL_WARNING

If set, and a file that bash is checking for mail has been accessed since the last time it was checked, the message The mail in [filename where mail is stored] has been read is printed.

MAILCHECK

This parameter specifies how often (in seconds) the shell will check for the arrival of mail in the files specified by the MAILPATH or MAIL parameters. The default value is 600 seconds (10 minutes). If set to zero, the shell will check before issuing each primary prompt.

MAILPATH

A colon-separated list of filenames. If this parameter is set, the shell informs the user of the arrival of mail in any of the specified files. Each filename can be followed by a % and a message that will be printed when the modification time changes. The default message is You have mail.

OLDPWD

Last working directory.

OPTARG

The value of the last option argument processed by the getopts built-in command.

OPTERR

If set to 1, displays error messages from the getopts built-in.

OPTIND

The index of the next argument to be processed by the getopts built-in command.

OSTYPE

Automatically set to a string that describes the operating system on which bash is executing. The default is system-dependent.

PATH

The search path for commands. It is a colon-separated list of directories in which the shell looks for commands. The default path is system-dependent, and is set by the administrator who installs bash. A common value is /usr/gnu/bin:/usr/local/bin:/usr/ucb:/bin:/usr/bin:.

PIPESTATUS

An array containing a list of exit status values from processes in the most recently executed foreground jobs in a pipeline.

PPID

Process ID of the parent process.

PROMPT_COMMAND

The command assigned to this variable is executed before the primary prompt is displayed.

PS1

Primary prompt string, by default $.

PS2

Secondary prompt string, by default >.

PS3

Selection prompt string used with the select command, by default #?.

PS4

Debug prompt string used when tracing is turned on, by default +. Tracing can be turned on with set –x.

PWD

Present working directory; set by cd.

RANDOM

Each time this parameter is referenced, a random integer is generated. The sequence of random numbers may be initialized by assigning a value to RANDOM. If RANDOM is unset, it loses its special properties, even if it is subsequently reset.

REPLY

Set when read is not supplied arguments.

SECONDS

Each time SECONDS is referenced, the number of seconds since shell invocation is returned. If a value is assigned to SECONDS, the value returned upon subsequent references is the number of seconds since the assignment plus the value assigned. If SECONDS is unset, it loses its special properties, even if it is subsequently reset.

SHELL

When the shell is invoked, it scans the environment for this name. The shell gives default values to PATH, PS1, PS2, MAILCHECK, and IFS. HOME and MAIL are set by login(1).

SHELLOPTS

Contains a list of enabled shell options, such as braceexpand, hashall, monitor, etc.

SHLVL

Incremented by one each time an instance of bash is started.

TMOUT

Specifies number of seconds to wait for input before exiting.

UID

Expands to the user ID of the current user, initialized at shell startup.


[a] Not available in bash versions prior to 2.x.

Setting Environment Variables.

To set environment variables, the export command is used either after assigning a value or when the variable is set. (See Table 13.15.) The declare built-in, given the –x option, will do the same. (Do not use the $ on a variable when exporting it.)

Table 13.15. The export Command and Its Options

Option

Value

--

Marks the end of option processing; the remaining parameters are arguments.

–f

Name–value pairs are treated as functions, not variables.

–n

Converts a global (exported) variable to a local variable. The variable will not be exported to child processes.

–p

Displays all the global variables.


FORMAT


export variable=value

variable=value; export variable

declare -x variable=value


Example 13.54.

export NAME=john

PS1= '\d:\W:$USER> ' ; export PS1

declare -x TERM=sun


Example 13.55.

1   $ export TERM=sun    # or declare -x TERM=sun

2   $ NAME="John Smith"

    $ export NAME

    $ echo $NAME

    John Smith

3   $ echo $$

    319             # pid number for parent shell

4   $ bash         # Start a subshell

5   $ echo $$

    340             # pid number for new shell

6   $ echo $NAME

    John Smith

7   $ declare -x NAME="April Jenner"

    $ echo $NAME

    April Jenner

8   $ exit          # Exit the subshell and go back to parent shell

9   $ echo $$

    319             # pid number for parent shell

10  $ echo $NAME

    John Smith


EXPLANATION

  1. The TERM variable is assigned sun. The variable is exported at the same time. Now, processes started from this shell will inherit the variable. You can use declare –x to do the same thing.

  2. The variable NAME is defined and exported to make it available to subshells started from the shell.

  3. The value of this shell's PID is printed.

  4. A new bash shell is started. The new shell is called the child. The original shell is its parent.

  5. The PID of the new bash shell is stored in the $$ variable and its value is echoed.

  6. The variable, set in the parent shell, was exported to this new shell and is displayed.

  7. The built-in declare function is another way to set a variable. With the –x switch, declare marks the variable for export. The variable is reset to April Jenner. It is exported to all subshells, but will not affect the parent shell. Exported values are not propagated upward to the parent shell.

  8. This bash child shell is exited.

  9. The PID of the parent is displayed again.

  10. The variable NAME contains its original value. Variables retain their values when exported from parent to child shell. The child cannot change the value of a variable for its parent.

13.10.6 Unsetting Variables

Both local and environment variables can be unset by using the unset command, unless the variables are set as read-only.

Example 13.56.

unset name; unset TERM


EXPLANATION

The unset command removes the variable from the shell's memory.

13.10.7 Printing the Values of Variables

The echo Command

The built-in echo command prints its arguments to standard output. Echo, with the –e option, allows the use of numerous escape sequences that control the appearance of the output. Table 13.16 lists the echo options and escape sequences.

Table 13.16. echo Options and Escape Sequences

Option

Meaning

–e

Allows interpretation of the escape sequences shown below

–E

Disables the interpretation of these escape characters, even on systems where they are interpreted by default[a]

–n

Suppresses newline at the end of a line of output

Escape Sequence

\a

Alert (bell)[a]

\b

Backspace

\c

Prints the line without a newline

\f

Form feed

\n

Newline

\r

Return

\t

Tab

\v

Vertical tab

\\

Backslash

\nnn

The character whose ASCII code is nnn (octal)


[a] Not available in bash versions prior to 2.x.

When using the escape sequences, don't forget to use the –e switch!

Example 13.57.

1   $ echo The username is $LOGNAME.

    The username is ellie.



2   $ echo -e "\t\tHello there\c"

                Hello there$



3   $ echo -n "Hello there"

    Hello there$


EXPLANATION

  1. The echo command prints its arguments to the screen. Variable substitution is performed by the shell before the echo command is executed.

  2. The echo command, with the –e option, supports escape sequences similar to those of the C programming language. The $ is the shell prompt.

  3. When the –n option is on, the line is printed without the newline. The escape sequences are not supported by this version of echo.

The printf Command.

The GNU version of printf[11] can be used to format printed output. It prints the formatted string in the same way as the C printf function. The format consists of a string that may contain formatting instructions to describe how the printed output will look. The formatting instructions are designated with a % followed by specifiers (diouxXfeEgGcs) where %f would represent a floating-point number and %d would represent a whole (decimal) number.

[11] On bash versions 2.x, printf is a built-in command.

To see a complete listing of printf specifiers and how to use them, type printf ––help at the command-line prompt. To see what version of printf you are using, type printf ––version. If you are using bash 2.x, the built-in printf command uses the same format as the executable version in /usr/bin.

FORMAT


printf format [argument...]


Example 13.58.

printf "%10.2f%5d\n" 10.5  25


Example 13.59.

1   $ printf --version

    printf (GNU sh-utils) 1.16



2   $ type printf

    printf is a shell builtin



3   $ printf "The number is %.2f\n" 100

    The number is 100.00



4   $ printf "%-20s%-15s%10.2f\n" "Jody" "Savage" 28

    Jody                Savage              28.00



5   $ printf "|%-20s|%-15s|%10.2f|\n" "Jody" "Savage" 28

    Jody                |Savage          |    28.00|



6   $ printf "%s's average was %.1f%%.\n" "Jody" $(( (80+70+90)/3 ))

    Jody's average was 80.0%.


Table 13.17. Format Specifiers for the printf Command

Format Specifier

Value

\"

Double quote

\0NNN

An octal character where NNN represents 0 to 3 digits

\\

Backslash

\a

Alert or beep

\b

Backspace

\c

Produce no further output

\f

Form feed

\n

Newline

\r

Return

\t

Horizontal tab

\v

Vertical tab

\xNNN

Hexadecimal character, where NNN is 1 to 3 digits

%%

A single %

%b

Argument as a string with \ escapes interpreted


EXPLANATION

  1. The GNU version of the printf command is printed.

  2. If using bash 2.x, printf is a built-in command.

  3. The argument 100 is printed as a floating-point number with only 2 places to the right of the decimal point as designated by the specification %.2f in the format string. Unlike C, there are no commas separating the arguments.

  4. The format string specifies that three conversions will take place: the first one is %–20s (a left-justified, 20-character string), next is %–15s (a left-justified, 15-character string), and last is %10.2f (a right-justified, 10-character floating-point number, one of whose characters is the period and the last two characters are the two numbers to the right of the decimal point). Each argument is formatted in the order of the corresponding % signs, so that string Jody corresponds to first %, string Savage corresponds to the second %, and the number 28 to the last % sign.

  5. This line is the same as line 4 except vertical bars have been added to demonstrate left and right justification of the strings.

  6. The printf command formats the string Jody and formats the result of the arithmetic expansion. (See "Arithmetic Expansion" on page 837.) Two percent (%%) signs are needed to print one percent sign (%).

13.10.8 Variable Expansion Modifiers (Parameter Expansion)

Variables can be tested and modified by using special modifiers. The modifier provides a shortcut conditional test to check if a variable has been set, and then assigns a value to the variable based on the outcome of the test. See Table 13.18 for a list of variable modifiers.

Table 13.18. Variable Modifiers

Modifier

Value

${variable:–word}

If variable is set and is non-null, substitute its value; otherwise, substitute word.

${variable:=word}

If variable is set or is non-null, substitute its value; otherwise, set it to word. The value of variable is substituted permanently. Positional parameters may not be assigned in this way.

${variable:+word}

If variable is set and is non-null, substitute word; otherwise, substitute nothing.

${variable:?word}

If variable is set and is non-null, substitute its value; otherwise, print word and exit from the shell. If word is omitted, the message parameter null or not set is printed.

${variable:offset}

Gets the substring of the value in variable starting at offset, where offset starts at 0 to the end of the string.[a]

${variable:offset:length}

Gets the substring of the value in variable starting at offset, length characters over.


[a] Not available on bash versions prior to 2.0.

Using the colon with any of the modifiers (, =, +, ?) checks whether the variable is not set or is null; without the colon, a variable set to null is considered to be set.

Example 13.60.

(Substitute Temporary Default Values)



1   $ fruit=peach

2   $ echo ${fruit:–plum}

    peach

3   $ echo ${newfruit:–apple}

    apple

4   $ echo $newfruit

5   $ echo $EDITOR       # More realistic EXAMPLE

6   $ echo ${EDITOR:-/bin/vi}

    /bin/vi

7   $ echo $EDITOR

8   $ name=

    $ echo ${name-Joe}

9   $ echo ${name:-Joe}

    Joe


EXPLANATION

  1. The variable fruit is assigned the value peach.

  2. The special modifier will check to see if the variable fruit has been set. If it has, the value is printed; if not, plum is substituted for fruit and its value is printed.

  3. The variable newfruit has not been set. The value apple will be temporarily substituted for newfruit.

  4. The setting was only temporary. The variable newfruit is not set.

  5. The environment variable EDITOR has not been set.

  6. The :– modifier substitutes EDITOR with /bin/vi.

  7. The EDITOR was never set. Nothing prints.

  8. The variable name is set to null. By not prefixing the modifier with a colon, the variable is considered to be set, even if to null, and the new value Joe is not assigned to name.

  9. The colon causes the modifier to check that a variable is either not set or is set to null. In either case, the value Joe will be substituted for name.

Example 13.61.

(Substitute Permanent Default Values)



1   $ name=

2   $ echo  ${name:=Peter}

    Peter

3   $ echo $name

    Peter

4   $ echo ${EDITOR:=/bin/vi}

    /bin/vi

5   $ echo $EDITOR

    /bin/vi


EXPLANATION

  1. The variable name is assigned the null value.

  2. The special modifier := will check to see if the variable name has been set. If it has been set, it will not be changed; if it is either null or not set, it will be assigned the value to the right of the equal sign. Peter is assigned to name because the variable is set to null. The setting is permanent.

  3. The variable name still contains the value Peter.

  4. The value of the variable EDITOR is set to /bin/vi.

  5. The value of the variable EDITOR is displayed.

Example 13.62.

(Substitute Temporary Alternate Value)



1   $ foo=grapes

2   $ echo ${foo:+pears}

    pears

3   $ echo $foo

    grapes

    $


EXPLANATION

  1. The variable foo has been assigned the value grapes.

  2. The special modifier :+ will check to see if the variable has been set. If it has been set, pears will temporarily be substituted for foo; if not, null is returned.

  3. The variable foo now has its original value.

Example 13.63.

(Creating Error Messages Based On Default Values)



1   $ echo ${namex:?"namex is undefined"}

    namex: namex is undefined

2   $ echo ${y?}

    y: parameter null or not set


EXPLANATION

  1. The :? modifier will check to see if the variable has been set. If not, the string to the right of the ? is printed to standard error, after the name of the variable. If in a script, the script exits.

  2. If a message is not provided after the ?, the shell sends a default message to standard error.

Example 13.64.

(Creating Substring[a])



1   $ var=notebook

2   $ echo ${var:0:4}

    note

3   $ echo ${var:4:4}

    book

4   $ echo ${var:0:2}

    no


EXPLANATION

  1. The variable is assigned the value, notebook.

  2. The substring of var starts at offset 0, the n in notebook, and has a length of 4 characters, ending at the e.

  3. The substring of var starts at offset 4, the b in notebook, and has a length of 4 characters, ending at the k.

  4. The substring of var starts at offset 0, the n in notebook, and has a length of 2 characters, ending at the o.

13.10.9 Variable Expansion of Substrings

Pattern-matching arguments are used to strip off certain portions of a string from either the front or end of the string. The most common use for these operators is stripping off pathname elements from the head or tail of the path. See Table 13.19.

Example 13.65.

1   $ pathname="/usr/bin/local/bin"

2   $ echo ${pathname%/bin*}

    /usr/bin/local


Table 13.19. Variable Expansion Substrings[a]

Expression

Function

${variable%pattern}

Matches the smallest trailing portion of the value of variable to pattern and removes it.

${variable%%pattern}

Matches the largest trailing portion of the value of variable to pattern and removes it.

${variable#pattern}

Matches the smallest leading portion of the value of variable to pattern and removes it.

${variable##pattern}

Matches the largest leading portion of the value of variable to pattern and removes it.

${#variable}

Substitutes the number of characters in the variable. If * or @, the length is the number of positional parameters.


[a] Not available on versions of bash prior to 2.x.

EXPLANATION

  1. The local variable pathname is assigned /usr/bin/local/bin.

  2. The % removes the smallest trailing portion of pathname containing the pattern /bin, followed by zero or more characters; that is, it strips off /bin.

Example 13.66.

1   $ pathname="usr/bin/local/bin"

2   $ echo ${pathname%%/bin*}

    /usr


EXPLANATION

  1. The local variable pathname is assigned /usr/bin/local/bin.

  2. The %% removes the largest trailing portion of pathname containing the pattern /bin, followed by zero or more characters; that is, it strips off /bin/local/bin.

Example 13.67.

1   $ pathname=/home/lilliput/jake/.bashrc

2   $ echo ${pathname#/home}

    /lilliput/jake/.bashrc


EXPLANATION

  1. The local variable pathname is assigned /home/lilliput/jake/.bashrc.

  2. The # removes the smallest leading portion of pathname containing the pattern /home; that is, /home is stripped from the beginning of the path variable.

Example 13.68.

1   $ pathname=/home/lilliput/jake/.bashrc

2   $ echo ${pathname##*/}

    .bashrc


EXPLANATION

  1. The local variable pathname is assigned /home/lilliput/jake/.bashrc.

  2. The ## removes the largest leading portion of pathname containing zero or more characters up to and including the last slash; that is, it strips off /home/lilliput/jake from the path variable.

Example 13.69.

1   $ name="Ebenezer Scrooge"

2   $ echo  ${#name}

    16


EXPLANATION

  1. The variable name is assigned the string Ebenezer Scrooge.

  2. The ${#variable} syntax displays the number of characters in the string assigned to the variable name. There are 16 characters in Ebenezer Scrooge.

13.10.10 Positional Parameters

Normally, the special built-in variables, often called positional parameters, are used in shell scripts when passing arguments from the command line, or used in functions to hold the value of arguments passed to the function. The variables are called positional parameters because they are referenced by numbers 1, 2, 3, and so on, representing their respective positions in the parameter list. See Table 13.20.

Table 13.20. Positional Parameters

Expression

Function

$0

References the name of the current shell script

$1–$9

Positional parameters 1–9

${10}

Positional parameter 10

$#

Evaluates to the number of positional parameters

$*

Evaluates to all the positional parameters

$@

Same as $*, except when double quoted

"$*"

Evaluates to "$1 $2 $3", and so on

"$@"

Evaluates to "$1" "$2" "$3", and so on


The name of the shell script is stored in the $0 variable. The positional parameters can be set, reset, and unset with the set command.

Example 13.70.

1   $ set punky tommy bert jody

    $ echo $*             # Prints all the positional parameters

    punky tommy bert jody

2   $ echo $1             # Prints the first position

    punky

3   $ echo $2 $3          # Prints the second and third position

    tommy bert

4   $ echo $#             # Prints the total number of positional parameters

    4

5   $ set a b c d e f g h i j k l m

    $ print $10           # Prints the first positional parameter followed by a 0

    a0

    $ echo ${10} ${11}    # Prints the 10th and 11th positions

    j k

6   $ echo $#

    13

7   $ echo $*

    a b c d e f g h i j k l m

8   $ set file1 file2 file3

    $ echo \$$#

    $3

9   $ eval echo \$$#

    file3

10  $ set --            # Unsets all positional parameters


EXPLANATION

  1. The set command assigns values to positional parameters. The $* special variable contains all of the parameters set.

  2. The value of the first positional parameter, punky , is displayed.

  3. The value of the second and third parameters, tommy and bert, are displayed.

  4. The $# special variable contains the number of positional parameters currently set.

  5. The set command resets all of the positional parameters. The original parameter list is cleared. To print any positional parameters beyond 9, use the curly braces to keep the two digits together. Otherwise, the value of the first positional parameter is printed, followed by the number appended to it.

  6. The number of positional parameters is now 13.

  7. The values of all the positional parameters are printed.

  8. The dollar sign is escaped; $# is the number of arguments. The echo command displays $3, a literal dollar sign followed by the number of positional parameters.

  9. The eval command parses the command line a second time before executing the command. The first time parsed by the shell, the print would display $3; the second time, after eval, the print displays the value of $3, file3.

  10. The set command with the –– option clears or unsets all positional parameters.

13.10.11 Other Special Variables

The shell has special variables consisting of a single character. The dollar sign preceding the character allows you to access the value stored in the variable. See Table 13.21.

Example 13.71.

1   $ echo The pid of this shell is $$

    The pid of this shell is 4725

2   $ echo The options for this shell are $–

    The options for this shell are imh

3   $ grep dodo /etc/passwd

    $ echo $?

    1

4   $ sleep 25&

    4736

    $ echo $!

    4736


Table 13.21. Special Variables

Variable

Meaning

$

The PID of the shell

The sh options currently set

?

The exit value of last executed command

!

The PID of the last job put in the background


EXPLANATION

  1. The $ variable holds the value of the PID for this process.

  2. The variable lists all options for this interactive bash shell.

  3. The grep command searches for the string dodo in the /etc/passwd file. The ? variable holds the exit status of the last command executed. Because the value returned from grep is 1, grep is assumed to have failed in its search. An exit status of 0 indicates a successful exit.

  4. The ! variable holds the PID number of the last command placed in the background. The & appended to the sleep command sends the command to the background.

    Previous Section  < Day Day Up >  Next Section