The $ symbol in Bash is used to replace the parameter name with the value represented by the parameter. For $, braces are optional, that is, $ A and $ {A} represent the same parameter.
$ {} There are several expressions with colons: $ {parameter:-word}
If parameter is null or not set, replace the entire parameter with the expression value of word.
$ {Parameter: = word}
If parameter is null or not set, the entire parameter replaces the expression value with word, and the parameter value is set to word.
$ {Parameter :? Word}
If parameter is null or not set, an error message is printed. Otherwise, the entire parameter replacement expression value is $ parameter.
$ {Parameter: + word}
If parameter is not null or is not set, replace the entire parameter with the expression value of word.
$ {Parameter: Offset }$ {parameter: offset: length}
The substring of the parameter value.
We can understand the following examples:
$ {}! There are several expressions: $ {! Prefix *} $ {! Prefix @}
Print out the parameter names with prefix
$ {! Name [@] }$ {! Name [*]}
This is for the name array and prints out the subscript of the name array.
We can understand the following examples:
$ {} Regular Expression: $ {parameter # Word }$ {parameter # Word}
Scan the word from the beginning and filter out the characters matching the regular expression of the word.
# For the shortest match, # For the longest match
$ {Parameter % word }$ {parameter % word}
Scan the word from the end and filter out the characters matching the regular expression of the word.
% Is the shortest match, and % is the longest match
We can understand the following examples:
$ {Parameter/pattern/string }$ {parameter // pattern/string}
Replace the pattern string corresponding to the parameter value with the string
/Indicates to replace only once
// Replace all
We can understand the following examples: