问题描述
在shell中,我运行下面的命令没有问题,
In a shell, I run following commands without problem,
LS -al
!LS
第二次调用到 LS
还列出与 -al
标记的文件。然而,当我把上面的脚本到bash脚本,投诉都扔,
the second invocation to ls
also list files with -al
flag. However, when I put the above script to a bash script, complaints are thrown,
!LS,命令没有找到。
如何实现脚本同样的效果?
how to realise the same effects in script?
推荐答案
您将需要打开两个命令的历史和风格的历史扩展在您的脚本(都是关闭的非交互shell默认):
You would need to turn on both command history and !-style history expansion in your script (both are off by default in non-interactive shells):
set -o history
set -o histexpand
扩展命令也反映到标准错误,就像在一个交互的shell。您可以prevent是通过打开 histverify
shell选项(禁用了javascript -s histverify
),但在非交互的shell,这似乎让历史扩展一个空操作。
The expanded command is also echoed to standard error, just like in an interactive shell. You can prevent that by turning on the histverify
shell option (shopt -s histverify
), but in a non-interactive shell, that seems to make the history expansion a null-op.
这篇关于使用"!"在脚本执行与相同参数的命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!