Linux Shell parameter extensions (Parameter Expansion)

Source: Internet
Author: User

This article main reference: 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 as: ${expression}. expression includes a variety of characters until the match is on '} '. The '} ' will not be checked to match when the following conditions occur:

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

2) in quotation marks, such as '} ';

3) in an arithmetic expression, a command substitution, or a variable extension, as in ${value}


The simplest form of parameter extension such as: ${parameter}

The parameter extension can be modified using the following pattern:

${ parameter:- [ word ]}
Use
Default Values. If parameter is unset or null, the expansion of word (or a empty string if word is Omitt ed) shall be substituted; Otherwise, the value of parameter shall be substituted.
When the value of
${parameter} is empty or not set, it is replaced with a [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 of word (or a empty string if word is Omitt ED) shall is assigned to parameter. In all cases, the final value of parameter shall is 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, the word value is used to assign a value to ${parameter} and replace 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 W Ord is omitted) shall was written to standard error and the shell exits with a non-zero exit status. Otherwise, the value of parameter shall 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 was omitted) shall be substituted.
The
expression returns NULL when the value of ${parameter} is empty or is not set. 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 is substituted. If parameter is ' * ' or ' @ 'and the result of the expansion is unspecified. If parameter is unset and set - u is in effect, the expansion shall fail.
An
expression returns the number of characters in a ${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, pattern matching notation (see pattern matching notation), rather than regular expression notation, Sha ll be used to evaluate the patterns. 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. 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 '% '.
match the suffix of ${parameter} with the pattern generated by [Word] and remove the minimum matching part
[[email protected] ~]$ echo ${cc}value not set[[email protected] ~]$ echo ${cc% ' et '}value not s
${ parameterpercent [ 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 pattern deleted.
match the suffix of ${parameter} with the pattern generated by [Word] 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 pattern deleted.
the last two are 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 extensions (Parameter Expansion)

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.