The main reference in this paper: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02
Other information:http://www.ibm.com/developerworks/cn/linux/l-bash-parameters.html
The parameter extension is represented by: ${expression}.
expression contains various characters until the match is on '} '. '} ' will not be checked to match when the following conditions occur:
1) after the escape character \, such as \{;
2) inside the lead, such as '} ';
3) in an arithmetic expression, a command substitution, or a variable extension. such as ${value}
The simplest way to expand the parameters is as follows: ${parameter}
You can change the parameter extension using the following pattern, for example:
${
parameter:-
[
Word
]}
Use Default Values. If?
parameter? is unset or null, the expansion?
Word? (or an empty string if?)
Word? is omitted) shall be substituted; Otherwise, the value of?
parametershall be substituted. When the value of ${parameter} is empty or not set, replace it with [Word] value. Otherwise, it is the value of the expression.
[[email protected] ~]$ Bb=3[[email protected] ~]$ echo ${aa}[[email protected] ~]$ echo ${bb}3[[email protected] ~]$ Echo ${aa-${bb}}3[[email protected] ~]$ aa=2[[email protected] ~]$ echo ${aa-${bb}}2
${
parameter:=
[
Word
]}
Assign Default Values. If?
parameter? is unset or null, the expansion?
Word? (or an empty string if?)
Word? is omitted) shall are assigned to?
parameter. In all cases, the final value of?
parameter? shall be substituted. Only variables, not positional parameters or special parameters, can is assigned in the this.When the value of ${parameter} is empty or not set. Use the [Word] value to give${parameter} assigns a value andReplace the last expression. Otherwise, it is the value of the expression.
[[email protected] ~]$ echo ${aa-${bb}}2[[email protected] ~]$ echo ${aa:=${bb}}2[[email protected] ~]$ echo ${cc}[[email Protected] ~]$ echo ${cc:=${bb}}3[[email protected] ~]$ echo ${cc}3
${
parameter:?
[word]} indicate Error if Null or Unset. If? parameter? is unset or null, the expansion of? word? (or a message indicating it is unset if?) wordis omitted) shall was written to standard error and the shell exits with a non-zero exit status. Otherwise, the value of? parametershall be substituted. An interactive shell need not exit. when the value of ${parameter} is empty or not set, use the [Word] value as the standard error output hint and exit the shell and return a non-0 state.
No , it is the value of the expression.
[Email protected] ~]$ echo ${CC:? " Value not set "}3[[email protected] ~]$ echo ${DD:?' Value not set '}-bash:dd:value not set
${
parameter:+
[
Word
]}
Use alternative Value. If?
parameter? is unset or NULL, and Null shall be substituted; Otherwise, the expansion of?
Word? (or an empty string if?)
Word? is omitted) shall was substituted. When the value of ${parameter} is empty or is not set, the expression returns NULL. Otherwise, replace the value of the expression with [Word].
[[email protected] ~]$ echo ${cc:+ ' value not set '}value not set[[email protected] ~]$ echo ${dd:+ ' value not set '}
${#parameter}
String Length. The length in characters of the value of?
parameter? shall be substituted. If?
parameter? is?' * '? or?' @ 'The result of the expansion is unspecified. If?
parameter? is unset and?
Set?
- u? is in effect, the expansion shall fail. Expression returns the number of characters in the ${parameter} value.
[[email protected] ~]$ echo ${#cc}1[[email protected] ~]$ echo ${#dd}0
the following four varieties of Parameter expansion provide for substring processing. In each case, the pattern matching notation (see? Pattern Matching Notation), rather than regular expression Notation, shall is used to evaluate the patterns. If? parameter ? ' # ' ,? ' * ' , or? ' @ ' , the result of the expansion is unspecified. If? parameter ? is unset and? set ? -u ? is in effect, the expansion shall fail. Enclosing the full parameter expansion string in Double-quotes shall not cause the following four varieties of pattern cha Racters to be quoted, whereas quoting characters within the braces shall has this effect. In each variety, if? word ? is omitted, and the empty pattern shall be used.
${parameter%[word]}
Remove smallest Suffix Pattern. The?
Word? shall is expanded to produce a pattern. The parameter expansion shall then result in?
parameter, with the smallest portion of the suffix matched by the?
pattern? deleted. If present,
Word? shall not begin with a unquoted?'% '. Use the pattern generated by [Word] to match the suffix of ${parameter} and remove the minimum matching part
[[email protected] ~]$ echo ${cc}value not set[[email protected] ~]$ echo ${cc% ' et '}value not s
${
parameter%%
[
Word
]}
Remove largest Suffix Pattern. The?
Word? shall is expanded to produce a pattern. The parameter expansion shall then result in?
parameter, with the largest portion of the suffix matched by the?
patterndeleted. Use the pattern generated by [Word] to match the suffix of ${parameter} and remove the maximum matching part
[[email protected] ~]$ echo ${cc%%t*}value no[[email protected] ~]$ echo ${cc%t*}value not SE
${
parameter#
[
Word
]}
Remove smallest Prefix Pattern. The?
Word? shall is expanded to produce a pattern. The parameter expansion shall then result in?
parameter, with the smallest portion of the prefix matched by the?
pattern? deleted. If present,?
Word? shall not begin with a unquoted?' # '.${
parameter##
[
Word
]}
Remove largest Prefix Pattern. The?
Word? shall is expanded to produce a pattern. The parameter expansion shall then result in?
parameter, with the largest portion of the prefix matched by the?
patterndeleted. The last two are respectively the [Word] minimum and maximum matches to remove the prefix
[[email protected] ~]$ echo ${cc#*t}set[[email protected] ~]$ echo ${cc##*t}[[email protected] ~]$ echo ${cc#v}alue not SE T
-
Linux Shell parameter extension (Parameter Expansion)