This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 5 years ago.
The community reviewed whether to reopen this question 2 years ago and left it closed:
Original close reason(s) were not resolved
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(13)
以空格开头的命令将不会包含在历史记录中。
请注意,这确实需要设置环境变量
$HISTCONTROL
。检查以下命令是否返回
ignorespace
或忽略两者
:要添加环境变量(如果缺少),可以将以下行添加到 Bash 配置文件中。例如,文件
%HOME/.bashrc
。再次采购配置文件后,空格前缀的命令将不会写入<代码>$HISTFILE。
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.Check that the following command returns
ignorespace
orignoreboth
:To add the environment variable if missing, the following line can be added to the Bash profile. E.g., to file
%HOME/.bashrc
.After sourcing the profile again, space-prefixed commands will not be written to
$HISTFILE
.在任何给定的 Bash 会话中,通过键入以下内容将历史文件设置为 /dev/null:
请注意,正如注释中所指出的,这不会将该会话中的任何命令写入历史记录!
请不要打扰系统管理员的辛勤工作;)
Doodad 的解决方案 更加优雅。只需取消设置变量:
unset HISTFILE
(谢谢!)In any given Bash session, set the history file to /dev/null by typing:
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 solution is more elegant. Simply unset the variable:
unset HISTFILE
(thanks!)John Doe 和 塞德里克·罗耶的回答。但是,这似乎对我有用。
您不应在历史记录中看到该命令的条目。
这是解释...
“history -d”从历史记录中删除提到的条目。
HISTCMD 存储下一个要执行的命令号。因此,(HISTCMD-1) 指的是最后执行的命令。
从 Bash 历史文件中删除特定行
An extension of John Doe's and Cédric ROYER's answer. But, this seems to work for me.
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.
The HISTCMD stores the command_number of the one to be executed next. So, (HISTCMD-1) refers to the last executed command.
Remove a certain line from Bash history file
如果您使用 Z shell (
zsh
),您可以运行:设置后,每个以空格开头的命令都将从历史记录中排除。
您可以在
.zshrc
中使用别名来打开/关闭:If you are using Z shell (
zsh
) you can run:After this is set, each command starting with a space will be excluded from history.
You can use aliases in
.zshrc
to turn this on/off:您可能会考虑使用没有历史记录的 shell,例如
(也许
/bin/dash
或/bin/sash
可能比/bin/sh
更合适code>)或者更好的是,使用 batch 实用程序,例如,
历史记录将包含
sh
或batch
这没有多大意义。You might consider using a shell without history, like perhaps
(perhaps
/bin/dash
or/bin/sash
could be more appropriate than/bin/sh
)Or even better, use the batch utility, e.g.,
The history would then contain
sh
orbatch
which is not very meaningful.您可以开始您的会话
,然后继续您的偷偷摸摸的行为。将 histfile 设置为
/dev/null
将被记录到历史文件中,但此条目将很容易被删除,并且不会显示任何痕迹(至少在历史文件中)。而且,这是非永久性的。
You can start your session with
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.Also, this is non-permanent.
正如Doodad 在评论中提到的,
unset HISTFILE 可以很好地做到这一点,但如果您还想删除一些历史记录,请执行 echo $HISTFILE
获取历史文件位置(通常是~/.bash_history
),取消设置 HISTFILE
,并编辑~/.bash_history
(或者任何HISTFILE
- 当然它现在未设置
所以你无法读取它)。然后你就编辑了你的历史记录,并且事实上你编辑了它!
As mentioned by Doodad in comments,
unset HISTFILE
does this nicely, but in case you also want to also delete some history, doecho $HISTFILE
to get the history file location (usually~/.bash_history
),unset HISTFILE
, and edit~/.bash_history
(or whateverHISTFILE
was - of course it's nowunset
so you can't read it).Then you've edited your history, and the fact that you edited it!
您还可以使用以下命令:
我认为这是一个非常可移植的命令。
You can also use the following command:
I think it's a very portable command.
有多种方法可以实现这一目标。这将历史文件的大小设置为 0:
这将历史文件设置为
/dev/null
,从而有效地禁用它:对于单个命令,您可以在命令前添加空格,但它不会保存在历史文件中。请注意,这要求您将
ignorespace
值包含在$HISTCONTROL
环境变量中(man bash 并搜索ignorespace
了解更多详细信息)。There are several ways you can achieve this. This sets the size of the history file to 0:
This sets the history file to
/dev/null
, effectively disabling it: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 forignorespace
for more details).你只需要运行:
$ set +o History
要查看更多信息,请运行:
$ 人集
You just need to run:
$ set +o history
To see more, run:
$ man set
这个命令可能会派上用场。这样不会记录执行的命令
This command might come in handy. This will not record the command that is executed
如果您想删除所有历史记录,包括您删除了所有历史记录,这非常方便!
This is handy if you want to erase all the history, including the fact that you erased all the history!