从shell脚本打印命令历史记录1的输出 [英] printing output of command history 1 from shell script
问题描述
这是我的问题,如果我输入以下内容,请从控制台输入
Here's my problem, from console if I type the below,
var=`history 1`
echo $var
我得到所需的输出.但是,当我在shell脚本中执行相同操作时,它没有显示任何输出.此外,对于其他命令,例如 pwd
, ls
等,脚本将显示所需的输出,而不会出现任何问题.
I get the desired output. But when I do the same inside a shell script, it is not showing any output. Also, for other commands like pwd
, ls
etc, the script shows the desired output without any issue.
推荐答案
默认情况下,在脚本中关闭了 history
,因此您需要将其打开:
By default history
is turned off in a script, therefore you need to turn it on:
set -o history
var=$(history 1)
echo "$var"
请注意,首选使用 $()
,而不是不推荐使用的反引号.
Note the preferred use of $( )
rather than the deprecated backticks.
但是,这只会查看当前进程的历史记录,也就是此shell脚本,因此它毫无用处.
However, this will only look at the history of the current process, that is this shell script, so it is fairly useless.
这篇关于从shell脚本打印命令历史记录1的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!