Sometimes we run bash commands with sensitive information. For example, running a shell script and passing passwords as the command-line arguments. In that case, it’s better to clear the bash history for security reasons.
有时,我们使用敏感信息运行bash命令。 例如,运行一个shell脚本并传递密码作为命令行参数。 在这种情况下,出于安全原因,最好清除bash历史记录。
清除Linux中的Bash历史记录 (Clear Bash History in Linux)
When we run any command in the bash shell, it gets stored in the .bash_history
file located in the user home directory. We can use history
command to print all the commands from this file. There are options to delete the entire history or any specific command from the bash history.
当我们在bash shell中运行任何命令时,该命令将存储在用户主目录中的.bash_history
文件中。 我们可以使用history
命令来打印该文件中的所有命令。 有一些选项可从bash历史记录中删除整个历史记录或任何特定命令。
1.删除重击历史记录以删除所有命令 (1. Delete the Bash History to remove all commands)
If you want to remove the bash history completely, you can run the history -c
command.
如果要完全删除bash历史记录,可以运行history -c
命令。
# history
...
737 history
738 vi .bash_history
739 cd
740 history
# history -c
# history
1 history
#

2.从Bash历史记录中删除特定命令 (2. Remove a Specific Command from the Bash History)
If you want to remove a specific entry from the bash history, use to history -d offset
command. The offset is the line number from the output of the history
command.
如果要从bash历史记录中删除特定条目,请使用history -d offset
命令。 偏移量是history
命令输出的行号。
[root@li1176-230 ~]# ls -1 | wc -l
3
[root@li1176-230 ~]# history
1 history
2 ls
3 cd
4 ls -1 | wc -l
5 history
[root@li1176-230 ~]# history -d 4
[root@li1176-230 ~]# history
1 history
2 ls
3 cd
4 history
5 history -d 4
6 history
[root@li1176-230 ~]#

如何在Mac OS中清除Bash历史记录 (How to Clear Bash History in Mac OS)
If you are using plain bash shell, you can use the -d or -c option to remove the bash history.
如果使用的是纯bash shell,则可以使用-d或-c选项删除bash历史记录。
bash-3.2$ history
1 history
2 cd
3 ls
4 pwd
5 history
bash-3.2$ history -d 4
bash-3.2$ history
1 history
2 cd
3 ls
4 history
5 history -d 4
6 history
bash-3.2$ history -c
bash-3.2$ history
1 history
bash-3.2$
ZSH清除记录 (ZSH Clear History)
ZSH is very popular these days. It’s built on top of bash shell and provides a lot of additional features to help developers. Unfortunately, the above history commands won’t work to clear ZSH shell history. It’s because the ZSH history is saved in ~/.zsh_history
file. If you want to remove any entry, open this file in VIM or any other editor and manually delete the entry from the file and save it.
ZSH最近很受欢迎。 它基于bash shell构建,并提供了许多其他功能来帮助开发人员。 不幸的是,以上历史记录命令无法清除ZSH Shell历史记录。 这是因为ZSH历史记录保存在~/.zsh_history
文件中。 如果要删除任何条目,请在VIM或任何其他编辑器中打开此文件,然后从文件中手动删除该条目并保存。
Note: The zsh history is loaded when the session starts, so the changes to the .zsh_history file won’t reflect in the current shell session. If you launch a new shell by opening a new window or tab, you will notice the changes.
注意 :在会话启动时会加载zsh历史记录,因此对.zsh_history文件的更改不会反映在当前的shell会话中。 如果通过打开新的窗口或选项卡启动新的外壳,您将注意到所做的更改。