如果我有像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