使用grep时如何防止密码或其他敏感信息存储在bash历史记录中?

Leo*_*Leo 22 security bash grep passwords

我最近不得不 grep 获取可能已保存在文件中的密码。我不希望该行出现在 bash 历史记录中。

使用 grep 时,如何防止密码或其他敏感信息存储在 bash 历史记录中?例如,如何让 grep 从标准输入或控制台读取模式?

小智 37

如果您放入HISTCONTROL=ignorespace.bashrc 并在命令名称前放置一个空格,则不会将其添加到您的历史记录中。

$ export HISTCONTROL=ignorespace
$ grep "passwd" secret_password_file.txt   # added to history
$  grep "passwd" secret_password_file.txt  # not added to history
Run Code Online (Sandbox Code Playgroud)

  • FWIW:标题中的问题和正文中的问题完全不同。我回答了标题中的问题。 (3认同)

ric*_*ici 6

为了完整起见,我在正文中回答了这个问题:如何让 grep 从标准输入读取模式:

您可以使用以下-f选项:

grep -f- /path/to/file
Run Code Online (Sandbox Code Playgroud)

这将从 中读取任意数量的模式stdin,每行一个。(-意味着stdin;您还可以指定一个带有模式的文件,每行一个。)grep将匹配/path/to/file与任何指定模式匹配的行。