在bash中运行命令而不保存历史记录


Answers:


54

在命令前添加空格。以空格开头的命令不会放入历史记录:

root@ubuntu-1010-server-01:~# echo foo
foo
root@ubuntu-1010-server-01:~# history 
    1  echo foo
    2  history 
root@ubuntu-1010-server-01:~#  echo bar
bar
root@ubuntu-1010-server-01:~# history 
    1  echo foo
    2  history 

猛男

  HISTCONTROL
         A  colon-separated  list of values controlling how commands are
         saved on the history list.  If  the  list  of  values  includes
         ignorespace,  lines  which begin with a space character are not
         saved in the history list.  A value of ignoredups causes  lines
         matching  the  previous history entry to not be saved.  A value
         of ignoreboth is shorthand for ignorespace and  ignoredups.   A
         value  of erasedups causes all previous lines matching the cur
         rent line to be removed from the history list before that  line
         is  saved.   Any  value  not  in the above list is ignored.  If
         HISTCONTROL is unset, or does not include a  valid  value,  all
         lines  read  by the shell parser are saved on the history list,
         subject to the value of HISTIGNORE.  The second and  subsequent
         lines  of a multi-line compound command are not tested, and are
         added to the history regardless of the value of HISTCONTROL.

1
比我对$ HISTIGNORE的回答要好。+1
马特·西蒙斯

1
每天学些新东西。
David Rickman

1
我通常将此设置/etc/profile为as HISTCONTROL=ignoreboth并导出HISTCONTROL变量。
ewwhite

这似乎对我不起作用?它似乎在写历史,但只包括前面的空间。486回声测试487历史记录488回声测试空间489历史记录
Peter

忽略最后的评论,看起来它可以在Ubuntu中运行,但不能在Debian /其他操作系统上运行。
彼得

3

还值得一提的是杀死当前登录会话而不是正常退出的技巧(因此没有机会保存历史记录)。当您登录共享的A / C时,这特别有用,您不必记住使用空格作为前缀,而只需杀死它即可结束会话。最简单的杀死方法是运行以下命令:

kill -9 0

Pid 0始终引用当前进程的PID,因此您基本上是向自己发送致命的终止信号。我也经常用它代替正常退出,因为我经常在正常退出时挂起会话,这可能是由于某些配置错误所致。


1

另一个解决方案是将历史记录文件设置为目录:

export HISTFILE=/
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.