Advanced parts of Bash variables
1. Variable expansion
${variable name} variable extension for example: ${filename}
$ (command) command replacement for example: $ (LS/)
$ (arithmetic) calculation extension for example: $ ((9+9))
Variable extension: A simple conditional judgment, the substitution value that assigns different variables to different conditions, is an implicit "if-then-else" program syntax
Basic syntax for judging whether a variable exists or not: ${the variable to be tested-default value}
Formula: There is a transmission exists, no, the default value is passed
Presence or absence or null: ${variable to be tested-: Default value}
If there is a pass, no or empty, the default value is passed in
Presence or absence or null: ${variables to be tested: = default}
Formulas: Set default values for variables that do not exist or are empty
Presence or absence or NULL, output hint: ${variables to be tested:? Prompt message}
Purpose: In the script, in order to detect the variable is empty or not present, output prompt information to prevent serious problems in the script
Testing the existence of variables: ${variables to be measured: + truth}
Purpose: To test the existence, existence and value of a variable, which is true
To organize your pictures, please see attachments
When a variable is a string, the use of the slice substring:
There are roughly two kinds: 1.${variable: Position start} 2.${variable: Position start: Length}
Take the length of the passed-in string: ${#变量名称} The extension of this method applies: ${#数组 [@]} or ${#数组 [*]} are the number of array elements taken
Style comparison application: ${variable # style} Starting from the far left, contrasting variable values, deleting "shortest match string"
${variable # #样式} ... "Best-fit string"
${variable% style} Starting from the far right, contrasting variable values, deleting "shortest match string"
${variable-percent-style} .... "Best-fit string"
Replace or delete a partial string
${variable/style/substitution string}: Replaces only the first matched string
${variable//style/replacement string}: Replace all matching strings
${variable/style}: Delete the first matching string
${variable//style}: Delete all matching strings
The bash variable for Linux