Suppose we define a variable as:
File=/dir1/dir2/dir3/my.file.txt
We can replace each other with ${} to get different values:
${file#*/}: Take out the first/its left string: dir1/dir2/dir3/my.file.txt
${file##*/}: Take out the last/and left string: my.file.txt
${file#*.} : Take out the first one. And the string to the left: file.txt
${file##*.} : Take out the last one. And the string to the left: txt
${file%/*}: Take off the last bar/its right string:/dir1/dir2/dir3
${file%%/*}: Remove the first/its right string: (null value)
${FILE%.*}: Take off the last one. And the string to the right:/dir1/dir2/dir3/my.file
${FILE%%.*}: Take out 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 plate # on the left of the $)
% is removed to the right (on the plate% on the right of the $)
The single symbol is the minimum match, and the two symbol is the maximum match.
${file:0:5}: Extracts the leftmost 5 bytes:/dir1
${file:5:5}: Extracts the 5 consecutive bytes to the right of the 5th byte: /dir2
We can also replace strings in variable values:
${file/dir/path}: Change the first dir to path:/ Path1/dir2/dir3/my.file.txt
${file//dir/path}: Swap all dir for Path:/path1/path2/path3/my.file.txt
with ${} also available for Different variable state assignments (no setting, null value, non-null value):
${file-my.file.txt}: If the $file is null, use My.file.txt as the default value. (Reservation not set and non-null value)
${file:-my.file.txt}: If the $file is not set or null, use My.file.txt as the default value. (leave non-null values)
${file+my.file.txt}: Use My.file.txt as the default value regardless of the $file value. (no value is reserved)
${file:+my.file.txt}: Use My.file.txt as the default value unless the $file is a null value. (leave null value)
${file=my.file.txt}: If $file is not set, use My.file.txt as the default and define the $file as a non-null value. (leave null and non-null values)
${file:=my.file.txt}: If the $file is not set or null, use My.file.txt as the default and define the $file as a non-null value. (leave non-null values)
${file?my.file.txt}: If the $file is not set, the My.file.txt output to STDERR. (leave null and non-null values))
${file:?my.file.txt}: If the $file is not set or null, the my.file.txt output to STDERR. (leave non-null values)
Also, ${#var} can calculate the length of the variable value:
${#file} can get 27 because/dir1/dir2/dir3/my.file.txt is just 27 bytes ...
Some of the supernatural powers of ${} in the shell