首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在bash脚本中使用历史命令?

如何在bash脚本中使用历史命令?
EN

Ask Ubuntu用户
提问于 2014-11-06 22:13:51
回答 2查看 7.6K关注 0票数 3

我尝试在bash脚本中使用history命令,但它没有工作。

bash文件的代码:

代码语言:javascript
复制
#!/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 ___"
EN

回答 2

Ask Ubuntu用户

发布于 2014-11-06 22:21:07

默认情况下,history在非交互式shell中由bash禁用。你必须启用它。

脚本应该如下所示:

代码语言:javascript
复制
#!/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.

参考资料:

票数 4
EN

Ask Ubuntu用户

发布于 2019-08-31 06:11:37

代码语言:javascript
复制
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 

注意:上面提到了脚本文件顶部的两行。现在历史指令将在历史中发挥作用。

票数 0
EN
页面原文内容由Ask Ubuntu提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://askubuntu.com/questions/546556

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档