如果我有像some/unknown/amount/of/sub/folder/file.txt
这样的字符串我怎样才能得到file.txt
子字符串,删除前部分,而长度未知。在shell脚本中获取子字符串
谢谢
编辑:文件名可以是任何长度,和子文件夹可以是任何水平。
如果我有像some/unknown/amount/of/sub/folder/file.txt
这样的字符串我怎样才能得到file.txt
子字符串,删除前部分,而长度未知。在shell脚本中获取子字符串
谢谢
编辑:文件名可以是任何长度,和子文件夹可以是任何水平。
使用basename
命令:
orig_path="some/unknown/amount/of/sub/folder/file.txt"
last_comp=$(basename $orig_path)
echo $last_comp
谢谢,真的帮助 – derrdji 2009-07-30 19:59:40
$ basename "some/unknown/amount/of/sub/folder/file.txt"
file.txt
一般地抽取子,你可以使用这个语法
$ hello="abcdef"
$ echo ${hello:1:3}
bcd
基名,一些/未知/数量/的/子/文件夹/文件.txt
虽然我同意正确的答案是调用basename,但在bash中,您也可以使用##来de从一个变量的起始处出现最长的字符串。
bash-3.2$ t=/this/is/a/path bash-3.2$ echo ${t##*/} path
你在用什么外壳? – 2009-07-30 19:48:37
呃...我不确定,GNU bash?你在问什么? o.O! – derrdji 2009-07-30 19:51:53