${}用于字符串的读取,提取和替换功能,可以使用${} 提取字符串
1.提取文件名:
[root@localhost test]# var=/mnt/aaa/test/test.txt
[root@localhost test]# echo ${var##*/}
test.txt
2.提取后缀:
[root@localhost test]# echo ${var##*.}
txt
3.提取文件名,不带后缀:
[root@localhost test]# tmp=${var##*/}
[root@localhost test]# echo $tmp
test.txt
[root@localhost test]# echo ${tmp%.*}
test
4.提取目录:
[root@localhost test]# echo ${var%/*}
/mnt/aaa/test
5.使用文件目录的专有命令basename和dirname:
注:basename命令使用
(),而不是
{}
- 提取文件名:
[root@localhost test]# echo $(basename $var)
test.txt
- 提取文件名,不带后缀:
[root@localhost test]# echo $(basename $var .txt)
test
- 提取目录:
[root@localhost test]# dirname $var
/mnt/aaa/test
[root@localhost test]# echo $(dirname $var)
/mnt/aaa/test
参考:shell学习..
本文介绍了如何在Shell脚本中提取文件名、后缀、不带后缀的文件名以及目录。讲解了使用basename和dirname这两个专有命令来操作文件路径,帮助读者更好地理解和应用Shell中的文件路径处理。
3906

被折叠的 条评论
为什么被折叠?



