Variable replacement and Expansion

Source: Internet
Author: User

 

1. Replace the variable $ {varname:-word}. If the variable varname exists and is not null, its value is returned. Otherwise, word is returned.
$ {Varname-word} if the variable varname exists, its value is returned. Otherwise, word is returned.

$ var=
$ echo ${var:-"hello"}
hello
$ echo ${var-"hello"}

$ unset var
$ echo ${var-"hello"}
hello
$


$ {Varname: + word} if the variable varname exists and is not null, word is returned. Otherwise, null is returned.

$ {Varname: = word} if the variable varname exists and is not null, its value is returned. Otherwise, it is set to word and word is returned.



$ var=
$ echo ${var:="hello"}
hello

$ echo ${var}

Hello

$ Var =

$ echo ${var="hello"}

$
Echo $ {var}


$

$ {Varname :? Word} if the variable varname exists and is not null, its value is returned. Otherwise, varname: Word is displayed and the current command or script is exited, if word is omitted, the default parameter null or not set is displayed.
$ {Varname? Word} if the variable varname exists, its value is returned. Otherwise, varname: Word is displayed and the current command or script is exited. If word is omitted, the default parameter null or not set is displayed.


$ var=
$ echo ${var:?"hello"}
bash: var: hello
$ echo ${var?"hello"}

$ unset var
$ echo ${var?"hello"}
bash: var: hello
$

Ii. pattern matching (Note: The pattern used here is the wildcard character pattern, rather than the regular expression). Assume that the variable Path =/home/Tolstoy/MEM/long. file. Name
$ {Variable # pattern} if the pattern matches at the beginning of the variable, the shortest part of the matching is deleted and the remaining part is returned.
Example: $ {path #/*/} result: Tolstoy/MEM/long. file. name $ {Variable # parttern} if the pattern matches at the beginning of the variable, the longest part of the matching is deleted and the remaining part is returned.
Example: $ {path ##/ */} result: Long. file. Name
$ {Variable % parttern} if the pattern matches the end of the variable, the shortest part of the matching is deleted and the remaining part is returned, for example, $ {PATH %. *} result:/home/Tolstoy/MEM/long. file $ {Variable % parttern} if the pattern matches the end of the variable, the longest part of the match is deleted and the remaining part is returned, for example, $ {PATH %. *} result:/home/Tolstoy/MEM/long
$ {Varname = word} if the variable varname exists, its value is returned. Otherwise, it is set to word and word is returned.
$ {Varname + word} if the variable varname exists, word is returned. Otherwise, null is returned.


$ var=
$ echo ${var:+"hello"}

$ echo ${var+"hello"}
hello
$ unset var
$ echo ${var+"hello"}

$


Iii. Special Cases

$ {# Var} indicates the wordString Length

$ #$ {# * }$ {#@} Indicates the location parameters.

For an array, $ {# array_name} indicates the length of the first element in the array.

For arrays, $ {# array_name [*]} and $ {# array_name [@]} represent the number of elements in the array.

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.