Suppose we define a variable as:
File=/dir1/dir2/dir3/my.file.txt
Different values can be replaced with ${}, respectively:
${file#*/}: Remove the first one/And the string to its left:Dir1/dir2/dir3/my.file.txt
${file##*/}: Erase the last one/And the string to its left:My.file.txt
${file#*.}: Remove the first one.And the string to its left:File.txt
${file##*.}: Erase the last one.And the string to its left:Txt
${file%/*}: Erase the last one/And the string to the right:/dir1/dir2/dir3
${file%%/*}: Remove the first one/And the string to the right:(Null value)
${file%.*} . And the string to the right: /dir1/dir2/dir3/my.file
${file%%.*} . /dir1/dir2/dir3/my
Memory by:
# is stripped to the left (on the keyboard # on the left side)
% is removed to the right (on the right of the keyboard% on $)
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}: Extract 5 bytes to the right of a continuous 5/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, use theMy.file.txtreturn value.(Do not process without setting and null value)
${file=my.file.txt}: If$fileWithout setting, useMy.file.txtreturn value, the same time$fileThe value ofMy.file.txt。(Null values and non-null values are not processed)
${file:=my.file.txt}: If$fileis not set or null, useMy.file.txtreturn value, the same time$fileThe value ofMy.file.txt ( non-null value is not processed )
${file?my.file.txt} $file No, it will My.file.txt output to Stderr ()
${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 be obtained , because /dir1/dir2/dir3/my.file.txt is a byte
Turn from:http://space.baidu.com.cn/ugo5/blog/item/c550bbc54d1644079c163dbd.html
##*,%%* Problems in the shell