如何在Linux和Mac中清除Bash历史记录

本文详细介绍了如何在Linux和MacOS系统中清除Bash历史记录,包括删除所有命令和特定命令的方法,以及在使用ZSH壳时如何进行操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
# 
Clear Bash History Linux
Clear Bash History Linux
清除Bash历史记录Linux

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 ~]#
Remove Specific Bash History Command
Remove Specific Bash History Command
删除特定的Bash历史记录命令

如何在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会话中。 如果通过打开新的窗口或选项卡启动新的外壳,您将注意到所做的更改。

翻译自: https://www.journaldev.com/38110/clear-bash-history-linux-mac

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值