1 . Deletion and substitution of variable contents
# The representation is deleted from the front, so here it starts / deletes ,* instead of 0 to infinitely more than any character
% Delete variable contents from behind
For example:echo ${path%:* bin} deletes the most a directory, that is, the string from: to the bin and is the shortest
Echo ${path/sbin/sbin}: two slashes in the middle of the old character, followed by the new character
How variables are set |
Description |
${ variable # keyword } |
If the data from the beginning of the variable content matches the "keyword", the shortest data is deleted |
${ variable # # keyword } |
If the data from the beginning of the variable content matches the "keyword", the shortest data is deleted |
${ variable % keyword } |
If the variable content from the tail forward data conforms to the "keyword", then the minimum data to be met is deleted |
${ variable % keyword } |
The longest data to be met is deleted if the contents of the variable content from the tail forward data conform to the "keyword" |
${ variable / old string / new string } |
If the contents of the variable conform to the old string, the first old string is replaced by the new string |
${ variable // old string / new string } |
If the contents of the variable conform to the old string, all old strings are replaced by the new string |
2. Testing of variables and content substitution
How variables are set |
Str is not set |
Str is an empty string |
STR has set a non-empty string |
VAR=${STR-EXPR} |
var=expr |
Var= |
Var= $str |
VAR=${STR:-EXPR} |
var=expr |
var=expr |
Var= $str |
VAR=${STR+EXPR} |
Var= |
var=expr |
var=expr |
VAR=${STR:+EXPR} |
Var= |
Var= |
var=expr |
VAR=${STR=EXPR} |
str=expr var=expr |
Str does not change Var= |
Str does not change Var= $str |
VAR=${STR:=EXPR} |
str=expr var=expr |
str=expr var=expr |
Str does not change Var= $str |
VAR=${STR?EXPR} |
Expr output to stderr |
Var= |
Var=str |
VAR=${STR:?EXPR} |
Expr output to stderr |
Expr output to stderr |
Var=str |
Shell and Shellscript Learning Note III (deletion, substitution and substitution of variable contents)