How To Delete or Not Keep a Command in Bash History

Delete a Command from Bash History

Here I give some test commands to fill history:

pontikis@ubuntu:~$ clear

pontikis@ubuntu:~$ echo 1
1
pontikis@ubuntu:~$ echo 2
2
pontikis@ubuntu:~$ echo 3
3
pontikis@ubuntu:~$ my-secret-command
my-secret-command: command not found

I can view the history using history command:

pontikis@ubuntu:~$ history
    1  clear
    2  echo 1
    3  echo 2
    4  echo 3
    5  my-secret-command
    6  history

So, to remove my-secret-command (line 5), I use -d option of history command:

pontikis@ubuntu:~$ history -d 5
pontikis@ubuntu:~$ history
    1  clear
    2  echo 1
    3  echo 2
    4  echo 3
    5  history
    6  history -d 5
    7  history

DO NOT KEEP a Command in Bash History

Start a command with SPACE if you DO NOT want to be included in Bash history:

pontikis@ubuntu:~$  my-secret-starts-with-space
my-secret-starts-with-space: command not found
pontikis@ubuntu:~$ history
1  clear
2  echo 1
3  echo 2
4  echo 3
5  history
6  history -d 5
7  history

Make sure that your .bashrc file contains something like this

# don't put duplicate lines or lines starting with space in the history.
# See bash(1) for more options
HISTCONTROL=ignoreboth

In other words, ensure that HISTCONTROL environmental variable is set to ignorespace or ignoreboth

Your comments are welcome!


Never Miss A Post!

Subscribe to our free NewsLetter