Linux Shell Parameter extension (Parameter Expansion), parameterexpansion

Source: Internet
Author: User

Linux Shell Parameter extension (Parameter Expansion), parameterexpansion

The main reference: http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_06_02

Other materials: http://www.ibm.com/developerworks/cn/linux/l-bash-parameters.html


The parameter extension format is $ {expression }. Expression contains various characters until matching'}'. When the following situations occur:'}' Will not be checked for matching:

1) after the escape character \, such \{;

2) in quotation marks, such '}';

3)In arithmetic expressions, commands or variable extensions, such as $ {value}


The simplest form of parameter extension is $ {parameter}

You can modify the parameter extension in the following mode:

$ { Parameter:- [ Word ]}
Use Default Values. If ParameterIs unset or null, the expansion Word(Or an empty string if WordIs omitted) shall be substituted; otherwise, the value ParameterShall be substituted.
When the $ {parameter} value is null or is not set, replace it with the [word] value. Otherwise, it is the value of this expression.
[hdfs@cdh51kdc ~]$ bb=3[hdfs@cdh51kdc ~]$ echo ${aa}[hdfs@cdh51kdc ~]$ echo ${bb}3[hdfs@cdh51kdc ~]$ echo ${aa-${bb}}3[hdfs@cdh51kdc ~]$ aa=2[hdfs@cdh51kdc ~]$ echo ${aa-${bb}}2
$ { Parameter: = [ Word ]}
Assign Default Values. If ParameterIs unset or null, the expansion Word(Or an empty string if WordIs omitted) shall be assigned Parameter. In all cases, the final value ParameterShall be substituted. Only variables, not positional parameters or special parameters, can be assigned in this way.
When the $ {parameter} value is null or is not set, use the [word] value to assign a value to $ {parameter} and replace the final expression. Otherwise, it is the value of this expression.
[hdfs@cdh51kdc ~]$ echo ${aa-${bb}}2[hdfs@cdh51kdc ~]$ echo ${aa:=${bb}}2[hdfs@cdh51kdc ~]$ echo ${cc}[hdfs@cdh51kdc ~]$ echo ${cc:=${bb}}3[hdfs@cdh51kdc ~]$ echo ${cc}3
$ { Parameter:? [ Word ]}
Indicate Error if Null or Unset. If ParameterIs unset or null, the expansion Word(Or a message indicating it is unset if WordIs omitted) shall be written to standard error and the shell exits with a non-zero exit status. Otherwise, the value ParameterShall be substituted. An interactive shell need not exit.
When the value of $ {parameter} is null or is not set, the [word] value is used as the standard error output prompt and the shell is exited and the status is not 0 is returned. Otherwise, it is the value of the expression.
[hdfs@cdh51kdc ~]$ echo ${cc:?"Value not set"}3[hdfs@cdh51kdc ~]$ echo ${dd:?"Value not set"}-bash: dd: Value not set

$ { Parameter: + [ Word ]}
Use Alternative Value. If ParameterIs unset or null, null shall be substituted; otherwise, the expansion Word(Or an empty string if WordIs omitted) shall be substituted.
When the $ {parameter} value is null or is not set, the expression returns null. Otherwise, replace the value of the expression with [word.
[hdfs@cdh51kdc ~]$ echo ${cc:+"Value not set"}Value not set[hdfs@cdh51kdc ~]$ echo ${dd:+"Value not set"}

$ {#Parameter}

String Length. The length in characters of the value ParameterShall be substituted. If ParameterIs '*'Or '@', The result of the expansion is unspecified. If ParameterIs unset and Set  -UIs in effect, the expansion shall fail.
Returns the number of characters in the $ {parameter} value.
[hdfs@cdh51kdc ~]$ echo ${#cc}1[hdfs@cdh51kdc ~]$ echo ${#dd}0

The following four varieties of parameter expansion provide for substring processing. in each case, pattern matching notation (see Pattern Matching Notation), rather than regular expression notation, shall be used to evaluate the patterns. ifParameterIs'#','*', Or'@', The result of the expansion is unspecified. IfParameterIs unset andSet -UIs in effect, the expansion shall fail. enclosing the full parameter expansion string in double-quotes shall not cause the following four varieties of pattern characters to be quoted, whereas quoting characters within the braces shall have this effect. in each variety, ifWordIs omitted, the empty pattern shall be used.

$ {Parameter%[Word]}

Remove Smallest Suffix Pattern. WordShall be expanded to produce a pattern. The parameter expansion shall then result in Parameter, With the smallest portion of the suffix matched by PatternDeleted. If present, WordShall not begin with an unquoted '%'.
Match the suffix of $ {parameter} in the [word] mode and remove the minimum matching part.
[hdfs@cdh51kdc ~]$ echo ${cc}Value not set[hdfs@cdh51kdc ~]$ echo ${cc%"et"}Value not s
$ { Parameter% [ Word ]}
Remove Largest Suffix Pattern. WordShall be expanded to produce a pattern. The parameter expansion shall then result in Parameter, With the largest portion of the suffix matched by PatternDeleted.
Match the suffix of $ {parameter} in the [word] mode and remove the largest matching part.
[hdfs@cdh51kdc ~]$ echo ${cc%%t*}Value no[hdfs@cdh51kdc ~]$ echo ${cc%t*}Value not se
$ { Parameter# [ Word ]}
Remove Smallest Prefix Pattern. WordShall be expanded to produce a pattern. The parameter expansion shall then result in Parameter, With the smallest portion of the prefix matched by PatternDeleted. If present, WordShall not begin with an unquoted '#'.
$ { Parameter## [ Word ]}
Remove Largest Prefix Pattern. WordShall be expanded to produce a pattern. The parameter expansion shall then result in Parameter, With the largest portion of the prefix matched by PatternDeleted.
The last two are the [word] minimum matching and maximum matching for removing the prefix respectively.
[hdfs@cdh51kdc ~]$ echo ${cc#*t}set[hdfs@cdh51kdc ~]$ echo ${cc##*t}[hdfs@cdh51kdc ~]$ echo ${cc#V}alue not set



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.