Replace Linux Shell Parameters
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.
$ {} Has the following 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:
Several expressions with regular expression matching:
$ {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: