Shell substitution operators and pattern matching operators
Substitution operators:
(1) ${varname:-word}: If varname exists and is not NULL, its value is returned, otherwise, word is returned.
(2) ${varname:=word}: If varname exists and is not NULL, its value is returned, otherwise, it is set to Word and returned.
(3) ${varname:?message}: Returns the value if VarName exists and is not NULL, otherwise, returns varname:message and exits the current command or script. Omitting the message will show the default information, parameter null or not set. Often used to catch errors that result from undefined variables.
(4) ${varname:+word}: Returns word if varname exists and is not NULL, otherwise NULL is returned. The existence of a use test variable, such as ${count:+1},count if it exists and is not NULL, returns 1 (also true)
The colon (:) inside the above operator is optional. If omitted, the existence of the above interpretation and non-null are changed to exist.
Pattern matching operators:
(1) ${variable#pattern}: If the pattern matches the beginning of the variable, the shortest part of the match is deleted and the remainder is returned.
(2) ${variable# #pattern}: If the pattern matches the beginning of the variable, the longest portion of the match is deleted and the remaining portion is returned.
(3) ${variable%pattern}: If the pattern matches at the end of the variable, the shortest part of the match is deleted and the remainder is returned.
(4) ${variable%%pattern}: If the pattern matches the end of the variable, the longest portion of the match is deleted and the remainder is returned.
Pattern matching operators are commonly used to slice parts of a path name, such as a directory prefix and a file name suffix.
Shell substitution operators and pattern matching operators