如何在bash脚本中使用'history-c'命令? [英] How to use 'history-c' command in a bash script?

查看:95
本文介绍了如何在bash脚本中使用'history-c'命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们知道,'history'命令显示Linux服务器的命令行历史记录,'history -c'是清除/删除此命令行历史记录的命令.

As we know, 'history' command displays the command line history of Linux server and 'history -c' is the command to clear/delete this command line history.

我必须通过我的bash脚本触发此命令.脚本如下,

I have to trigger this command through my bash script. Script is as follows,

#! /bin/bash
var=`history -c`
if [ $? -eq 0 ]
then
echo "cleared"
echo $var
fi

输出如下:

  cleared

尽管输出已清除",但history-c并未删除历史记录.

Though its printing "cleared" as the output, history-c is not deleting the history.

如果您可以指导/建议我如何实现此目标,那就太好了,例如,在我的bahs脚本中使用"history-c"命令删除命令行历史记录.或者还有其他方法可以删除命令我的bash脚本记录行历史记录.

It would be great if you can guide/suggest on how i can achieve this, i.e using "history-c" command in my bahs script to delete command line history.Or is there any other way in which i can delete command line history through my bash script.

感谢&问候,海军蓝

Thanks & Regards, Navya

推荐答案

history -c 清除当前shell的历史记录,并且不删除〜/.bash_history.

history -c clears the history for the current shell, and does not delete ~/.bash_history.

但是,当您运行脚本时,当前外壳会创建一个新的外壳来运行脚本,并在脚本完成后退出该外壳.

But when you run a script the current shell creates a new shell to run the script in and exits that shell when the script is done.

相反,要在当前 shell 中执行脚本,您必须获取脚本的源代码.如果脚本的名称是foo.sh,请尝试运行../foo.sh

Instead, to execute a script in the current shell you have to source the script. If the name of your script is foo.sh, try running . ./foo.sh

但是,无论哪种情况,您编写的脚本都不会执行该命令.修改它像这样:

But in either case, the script that you've written does not execute the command. Modify it to something like this:

#! /bin/bash
history -c
if [ $? -eq 0 ]
then
echo "cleared"
fi

这篇关于如何在bash脚本中使用'history-c'命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆