Bash 从脚本变量中提取文件名

Bash 从脚本变量中提取文件名,bash,macos,Bash,Macos,我有一个像这样的批处理文件 对于bin/$1*.txt中的f;不要重复$f;完成 我只想回显文件名-不是bin/temp.txt或temp.txt-只是temp 如何从$f提取文件名?如果后缀始终相同,您可以使用basename-s.txt$f.参数展开 $: f=bin/temp123.txt # an example you might have $: x="${f#bin/}" # asign to x without bin/ prefix $: echo $x temp123.

我有一个像这样的批处理文件

对于bin/$1*.txt中的f;不要重复$f;完成

我只想回显文件名-不是bin/temp.txt或temp.txt-只是temp


如何从$f提取文件名?

如果后缀始终相同,您可以使用basename-s.txt$f.

参数展开

$: f=bin/temp123.txt # an example you might have
$: x="${f#bin/}"     # asign to x without bin/ prefix
$: echo $x
temp123.txt
$: echo ${x%.txt}    # show x without .txt suffix
temp123