Suppose we define a variable as:
Copy the Code code as follows:
File=/dir1/dir2/dir3/my.file.txt
Different values can be replaced with ${}, respectively:
Copy the Code code as follows:
${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:
Copy the Code code as follows:
# 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:
Copy the Code code as follows:
${file/dir/path}: Replace the first dir with a path:/path1/dir2/dir3/my.file.txt
${file//dir/path}: Replace all dir with Path:/path1/path2/path3/my.file.txt
(2)
# # #后面如果直接跟匹配的字符串, will delete the previous or left all strings, the main wildcard position (# # # * must be in front such as *llo); match the complete string only the first, the same effect, if the complete string is not the first, output as is
Percent% after the string that matches directly with the match, it deletes all the strings from the front or right side, the main wildcard position (% percent * must be in the back such as he*), with the full string as the output
Like what
A= "This is hello a maomaochong hello this!"
#echo ${a#hello}//this is hello a maomaochong hello this!
#echo ${a# #hello}//this is hello a maomaochong hello this!
PNS #echo ${a#*llo}//a maomaochong Hello this!
#echo ${a##*llo}//this!
${a%this}//this Echo is hello a maomaochong hello this!
echo ${a%%this}//this is hello a maomaochong hello this!
#echo ${a%he*}//This is Hello a maomaochong
#echo ${a%%he*}//this is
//
${}, # #和 in the shell