file_jpg="sample.jpg"
name=${file_jpg%.*}
echo File name is $name
File name is sample
2.
file_jpg="sample.jpg"
extension=${file_jpg#*.}
echo Extension is $extension
File name is jpg
3.
file_name=""
echo ${file_name%%.*}
www
4.
file_name=""
echo ${file_name##*.}
com
說明:
${file_jpg%.*}的含義是:從$file_jpg中刪除位于%右側(cè)的通配符所匹配的字符串,通配符從右向左進(jìn)行匹配。
${file_jpg#*.}的含義是:從$file_jpg中刪除位于#右側(cè)的通配符所匹配的字符串,通配符從左向右進(jìn)行匹配。
%屬于非貪婪操作,它從右到左找出匹配通配符的最短結(jié)果。
%%屬于非貪婪操作,它從右到左找出匹配通配符的最長結(jié)果。
#屬于非貪婪操作,它從左到右找出匹配通配符的最短結(jié)果。
##屬于非貪婪操作,它從左到右找出匹配通配符的最長結(jié)果。