Last Updated: February 25, 2016
· gregswift

Never have to clear your password from your bash history again!

You could just be awesome and never type it in your bash shell in the first place. But if you end up doing it on occasion here is an easy way to have bash ignore it.

Always start your password with an exclamation point (!)

Then build a secure password after the ! like you normally would.

Why this works

The ! is a special function call in many shells that can be used to call previous commands from your shell history by their number in the history. For example:

[me@boxen tmp]$ echo "this is a command"
this is a command
[me@boxen tmp]$ cd ../tmp
[me@boxen tmp]$ cat file
[me@boxen tmp]$ history | tail -4
10  echo "this is a command"
11  cd ../tmp
12  cat file
13  history | tail -4
[me@boxen tmp]$ !12
cat file
[me@boxen tmp]$ !password
bash: !password: event not found
[me@boxen tmp]$ history | tail -4
12  cat file
13  history | tail -4
14  cat file
15  history | tail -4

As you can see the reference to a previous event populates the history with that event, but the password has no matching reference, and thus disappears.