Linux 执行命令而不将其保留在历史记录中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8473121/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Execute command without keeping it in history
提问by Jan Vorcak
I want to execute some commands but don't want to store them in the command history. So that nobody will be able to search it in the .bash_history
file.
我想执行一些命令但不想将它们存储在命令历史记录中。这样任何人都无法在.bash_history
文件中搜索它。
Is there any way how to execute bash commands this way?
有没有办法以这种方式执行bash命令?
采纳答案by u-punkt
Start your command with a space and it won't be included in the history.
以空格开头的命令不会包含在历史记录中。
Be aware that this does require the environment variable $HISTCONTROL
to be set.
请注意,这确实需要设置环境变量$HISTCONTROL
。
Check that the following command returns
ignorespace
orignoreboth
#> echo $HISTCONTROL
To add the environment variable if missing, the following line can be added to the bash profile. E.g.
%HOME/.bashrc
export HISTCONTROL=ignorespace
检查以下命令是否返回
ignorespace
或ignoreboth
#> echo $HISTCONTROL
要添加缺少的环境变量,可以将以下行添加到 bash 配置文件中。例如
%HOME/.bashrc
export HISTCONTROL=ignorespace
After sourcing the profile again space prefixed commands will not be written to $HISTFILE
再次获取配置文件后,将不会写入以空格为前缀的命令 $HISTFILE
回答by Miquel
In any given Bash session, set the history file to /dev/null by typing:
在任何给定的 Bash 会话中,通过键入以下内容将历史文件设置为 /dev/null:
export HISTFILE=/dev/null
Note that, as pointed out in the comments, this will not write any commands in that session to the history!
请注意,正如评论中所指出的,这不会将该会话中的任何命令写入历史记录!
Just don't mess with your system administrator's hard work, please ;)
请不要打扰您系统管理员的辛勤工作;)
Doodad's solutionis more elegant. Simply unset the variable: unset HISTFILE
(thanks!)
Doodad 的解决方案更优雅。只需取消设置变量:(unset HISTFILE
谢谢!)
回答by pgl
There are several ways you can achieve this. This sets the size of the history file to 0:
有几种方法可以实现这一点。这将历史文件的大小设置为 0:
export HISTFILESIZE=0
This sets the history file to /dev/null
, effectively disabling it:
这/dev/null
会将历史文件设置为,从而有效地禁用它:
export HISTFILE=/dev/null
For individual commands, you can prefix the command with a space and it won't be saved in the history file. Note that this requires you have the ignorespace
value included in the $HISTCONTROL
environment variable (man bash and search for ignorespace
for more details).
对于单个命令,您可以在命令前加一个空格,它不会保存在历史文件中。请注意,这要求您将ignorespace
值包含在$HISTCONTROL
环境变量中(man bash 并搜索ignorespace
更多详细信息)。
回答by Basile Starynkevitch
You might consider using a shell without history, like perhaps
您可能会考虑使用没有历史记录的外壳,例如
/bin/sh << END
your commands without history
END
(perhaps /bin/dash
or /bin/sash
could be more appropriate than /bin/sh
)
(也许/bin/dash
或者/bin/sash
可能比 更合适/bin/sh
)
or even better use the batchutility e.g
甚至更好地使用批处理实用程序,例如
batch << EOB
your commands
EOB
The history would then contain sh
or batch
which is not very meaningful
然后历史将包含sh
或batch
不是很有意义
回答by Cédric ROYER
You can also use the following command:
您还可以使用以下命令:
echo toto; history -d $(history | sed -n '$s/\s*\([0-9]*\)\s*.*$//p')
I think it's a very portable command.
我认为这是一个非常便携的命令。
回答by cnd
This is handy if you want to erase all the history, including the fact that you erased all the history!
如果您想删除所有历史记录,这很方便,包括您删除了所有历史记录的事实!
rm .bash_history;export HISTFILE=/dev/null;exit
回答by John Doe
echo "discreet";history -d $(history 1)
回答by Rangaraj KS
An extension of @John Doe & @user3270492's answer. But, this seems to work for me.
@John Doe & @user3270492 答案的扩展。但是,这似乎对我有用。
<your_secret_command>; history -d $((HISTCMD-1))
You should not see the entry of the command in your history.
您不应该在历史记录中看到该命令的条目。
Here's the explanation..
这是解释..
The 'history -d' deletes the mentioned entry from the history.
'history -d' 从历史记录中删除提到的条目。
The HISTCMD stores the command_number of the one to be executed next. So, (HISTCMD-1) refers to the last executed command.
HITCMD 存储下一个要执行的命令的编号。因此,(HITCMD-1) 指的是最后执行的命令。
回答by drkvogel
As mentioned by Doodad in comments, unset HISTFILE
does this nicely, but in case you also want to also delete some history, do echo $HISTFILE
to get the history file location (usually ~/.bash_history
), unset HISTFILE
, and edit ~/.bash_history
(or whatever HISTFILE
was - of course it's now unset
so you can't read it).
正如Doodad 在评论中提到的,unset HISTFILE
这样做很好,但如果您还想删除一些历史记录,请执行echo $HISTFILE
获取历史记录文件位置(通常~/.bash_history
)unset HISTFILE
、 和编辑~/.bash_history
(或其他任何内容HISTFILE
- 当然现在unset
这样您就可以了)不读)。
$ echo $HISTFILE # E.g. ~/.bash_history
$ unset HISTFILE
$ vi ~/.bash_history # Or your preferred editor
Then you've edited your history, and the fact that you edited it!
然后您编辑了您的历史记录,以及您编辑它的事实!
回答by Christian
You can start your session with
你可以开始你的会话
export HISTFILE=/dev/null ;history -d $(history 1)
then proceed with your sneaky doings. Setting the histfile to /dev/null
will be logged to the history file, yet this entry will be readily deleted and no traces (at least in the history file) will be shown.
然后继续你的偷偷摸摸的行为。将 histfile 设置为/dev/null
将被记录到历史文件中,但此条目将很容易被删除并且不会显示任何痕迹(至少在历史文件中)。
Also, this is non-permanent.
此外,这是非永久性的。