Suppose we define a variable as:
File=/dir1/dir2/dir3/my.file.txt
can be replaced by ${} with different values:
${file#*/}: Delete the first/its left string: dir1/dir2/dir3/my.file.txt
${file##*/ }: Delete the last/ and its left string: my.file.txt
${file#*.} : Remove the first one. and its left string: file.txt
${file##*.} : Remove the last one. and its left string: txt
${file%/*}: Remove the last / and its right string:/dir1/dir2/dir3< BR style= "padding:0px; margin:0px ">${file%%/*}: Delete the first/ and its right string: (null value)
${file%.*}: Delete the last . and its right string:/dir1/dir2/dir3/my.file
${file%%.*}: Remove the first . 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}: Extracts the leftmost5bytes:/dir1
${file:5:5}: Extract Section5byte to the right of a continuous5bytes:/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
With ${} You can also assign values to different variable states (no settings, null values, non-null values):
${file-my.file.txt}: If $file is not set, use My.file.txt to return the value. (null and non-null values are not processed)
${file:-my.file.txt}: If $file is not set or null, use My.file.txt as the return value. (Non-null value is not processed)
${file+my.file.txt}: If the $file is set to null or non-null values, use My.file.txt as the return value. (not to be processed)
${file:+my.file.txt}: If $file is a non-null value, use My.file.txt as the return value. (No processing when not set and null value)
${file=my.file.txt}: If the $file is not set, use My.file.txt as the return value, and will $file the value of My.file.txt. (null and non-null values are not processed)
${file:=my.file.txt}: If the $file is not set or NULL, the my.file.txt is used as the return value, and the value of the $file is my.file.txt. (Non-null value is not processed)
${file?my.file.txt}: If $file is not set, the my.file.txt will be output to STDERR. (null 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} to calculate the length of the variable value:
${#file} can get 27 because/dir1/dir2/dir3/my.file.txt is 27 bytes
Some tips for Shell variables