${parameter:-value} |
You can use ${parameter:-value} in
any input to the shell. If parameter currently
has a value and the value is not null (for example, a string without
characters), the foregoing construct stands for the parameter's value.
If the value of the parameter is null, the construct is replaced with
the value shown in the brace brackets. For
example, a shell script might contain: SHELL=${SHELL:-/bin/sh}
If
the SHELL variable currently has a value, this simply assigns SHELL its
own current value. However, if the value of SHELL is null,
the given assignment will have the value of /bin/sh. The value
after :– can be thought of as a backup value
in case the parameter itself does not have a value. As another example,
consider: cp $1 ${2:-$HOME}
(This might occur
in a shell script.) If both positional parameters are present and
have a nonnull value, the copy command is just: cp $1 $2
However,
if you call the shell script without specifying a second positional
parameter, it uses the backup value of $HOME. The result is
equivalent to: cp $1 $HOME
|
${parameter:=value} |
The expansion form ${parameter:=value} is
similar to the previous form; the difference is that if the given parameter does
not currently have a value, the given value is
assigned to parameter, and then the new
value of parameter is used. Thus the := form actually
assigns a value if the parameter does not
already have one. In this case, parameter must
be a variable; it cannot be a positional parameter. |
${parameter:?message} |
The expansion ${parameter:?message} is
related to the previous two forms. If the value of the given parameter is
null, the given message is displayed. If
the construct is being used inside a shell script, the script ends
with an error status. For example, you might have: cp $1 ${2:?"Must specify a directory name"}
In
this case, the message following the ? is displayed
if there is no second positional parameter. If you omit the message,
the shell prints a standard message. For example, you could just enter:
cp $1 ${2:?}
to get the standard error message. |
${parameter:+replacement} |
The construct ${parameter:+replacement} might
be thought of as the opposite of the preceding expansions. If parameter has
not been assigned a value, or has a null value, this construct is
just the null string. If parameter does have
a value, the value is ignored and the replacement value
is used in its place. Thus, if a shell script contains: echo ${1:+"There was a parameter"}
the echo command
displays: There was a parameter
if the script
was invoked with a parameter. If no parameter was specified, the echo command
has nothing to echo. |
${parameter#pattern} |
The construct ${parameter#pattern} is
evaluated by expanding the value of parameter and
then deleting the smallest leftmost part of the expansion that
matches the given pattern of pathname wildcard
characters. For example, suppose that the variable NAME stands
for a file name. You might use: ${NAME#*/}
to
remove the highest-level directory from the pathname. If: NAME="user/dir/subdir/file.c"
then:
${NAME#*/}
expands to: dir/subdir/file.c
|
${parameter##pattern} |
The construct ${parameter##pattern} removes
the largest leftmost part that matches the pattern. For example,
if: NAME="user/dir/subdir/file.c"
then: ${NAME##*/}
yields:
file.c
The wildcard character * stands
for any sequence of characters. In this situation, it stands for everything
up to the final slash. |
${parameter%pattern} |
The construct ${parameter%pattern} removes
the smallest rightmost part of the parameter expansion that
matches pattern. Thus if: NAME="user/dir/subdir/file.c"
then:
${NAME%.?}
stands for: user/dir/subdir/file
|
${parameter%%pattern} |
Similarly, ${parameter%%pattern} stands
for the expansion of parameter without the longest
rightmost string that matches pattern.
Using the previous example of NAME, ${NAME%%/*}
stands
for: user
|