假设我们在Linux上运行以下脚本:
/tmp/start.sh
运行它之后,是否有任何方法可以检索上次执行的命令的名称?
预期产量
start.sh
在交互式bash
外壳中使用历史记录扩展:
$ /some/path/script.sh
$ printf 'Basename of last command line: %s\n' "$(basename "!!")"
printf 'Basename of last command line: %s\n' "$(basename "/some/path/script.sh")"
Basename of last command line: script.sh
该!!
是一个历史扩展事件标志,这将是最后一个命令来代替。请参见手册中的“历史扩展”部分bash
。
请注意,!!
它将扩展到完整的命令行。
在shell脚本中,您总是知道以前的脚本是什么,因为您只是键入了它。
/some/path/script.sh
echo 'script.sh just finished'
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句