If we define a variable as:
File=/dir1/dir2/dir3/my.file.txt
Can be replaced with ${} to get different values:
${file#*/}: Delete the first/its left string: dir1/dir2/dir3/my.file.txt
${file##*/}: Remove the last/and left string: my.file.txt
${file#*.} : Remove the first one. And the string to the left: file.txt
${file##*.} : Remove the last one. And the string to the left: txt
${file%/*}: Remove the last/And right string:/dir1/dir2/dir3
${file%%/*}: Delete the first/its right string: (null value)
${file%.*}: Remove the last one. And the string to the right:/dir1/dir2/dir3/my.file
${file%%.*}: Remove the first one. And the string to the right:/dir1/dir2/dir3/my
The methods of memory are:
# is to remove the left side (on the keyboard # on the left side)
% is removed to the right (on the keyboard% on the right side)
The single symbol is the minimum match; two symbols are the maximum match
${file:0:5}: Extract the leftmost 5 bytes:/dir1
${file:5:5}: Extracts the 5th byte to the right of 5 consecutive bytes:/DIR2
You can also replace the string in the value of the variable:
${file/dir/path}: The firstdirreplaced byPath:/path1/dir2/dir3/my.file.txt
${file//dir/path}: Will alldirreplaced byPath:/path1/path2/path3/my.file.txt
Use ${ } You can also assign values for different variable states(no setting, null value, non-null value):
${file-my.file.txt}: If$fileWithout a setting, useMy.file.txtreturns a value. (null values and non-null values are not processed)
${file:-my.file.txt}: If$fileWithout a set or null value, use theMy.file.txtreturn value.
(non-null values are not processed)
${file+my.file.txt}: If$fileNULL or a non-null value, both use theMy.file.txtreturn value. (without setting it up for processing .)
${file:+my.file.txt}: If$fileAs a non-null value. UseMy.file.txtreturn value. (do not process without setting and null value)
${file=my.file.txt}: If$fileNot set. UseMy.file.txtreturn value. in the meantime,$fileThe value ofMy.file.txt. (null values and non-null values are not processed)
${file:=my.file.txt}: If$fileNot set or null value. UseMy.file.txtreturn value, the same time$fileThe value ofMy.file.txt. (non-null values are not processed)
${file?my.file.txt}: If$fileNo , it willMy.file.txtinput toSTDERR. (null values and non-null values are not processed)
${file:?my.file.txt}: If the $file is not set or null, the my.file.txt output to STDERR.
(Non-null value is not processed)
${#var} the length of the variable value can be calculated:
${#file} can get 27. Since/dir1/dir2/dir3/my.file.txt is 27 bytes
${},# #和 percent usage in the shell