Shell获取文件的文件名和扩展名的例子

这篇文章主要介绍了Shell获取文件的文件名和扩展名的例子,简明版的代码实例,看了就懂,需要的朋友可以参考下

basename example.tar.gz .tar.gz
# => example
 
FILE="example.tar.gz"
 
echo "${FILE%%.*}"
# => example
 
echo "${FILE%.*}"
# => example.tar
 
echo "${FILE#*.}"
# => tar.gz
 
echo "${FILE##*.}"
# => gz
 
# 在bash中可以这么写
filename=$(basename "$fullfile")
extension="${filename##*.}"
filename="${filename%.*}"

#提取最后一个"."后面的字符,即扩展名

原文地址:https://www.cnblogs.com/AloneSword/p/4489043.html