我尝试在bash脚本中使用history命令,但它没有工作。
bash文件的代码:
#!/bin/bash
# Copy history to file history
#cd /media/saleel_almajd/Study/linux/my_scripts/
echo "start Copy history to /media/saleel_almajd/Study/linux/my_scripts/history.txt"
export HISTTIMEFORMAT='%F %T ' #to make history and date apear
history >> /media/saleel_almajd/Study/linux/my_scripts/history.txt
#cd
echo "... copy done ..."
#history -c
echo "..... history cleared ..... "
echo "___ Done Successfully ___"发布于 2014-11-06 22:21:07
默认情况下,history在非交互式shell中由bash禁用。你必须启用它。
脚本应该如下所示:
#!/bin/bash
HISTFILE=~/.bash_history # Set the history file.
HISTTIMEFORMAT='%F %T ' # Set the hitory time format.
set -o history # Enable the history.
file="/media/saleel_almajd/Study/linux/my_scripts/history.txt"
history >> $file # Save the history.
history -cw # Clears the history of the current session.参考资料:
发布于 2019-08-31 06:11:37
history command is disabled by default on bash script that's why even
history command won't work in .sh file. for its redirection, kindly
redirect bash_history file inside the .sh file.
or history mechanism can be enabled also by mentioning history file and
change run-time parameters as mentioned below
#!/bin/bash
HISTFILE=~/.bash_history
set -o history 注意:上面提到了脚本文件顶部的两行。现在历史指令将在历史中发挥作用。
https://askubuntu.com/questions/546556
复制相似问题