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.