Published at
Updated at
Reading time
1min
This post is part of my Today I learned series in which I share all my web development learnings.

You've probably been in this situation. You executed a destructive command to delete files or clear a database. Everything went well. A few days later, you navigate your terminal history to find a similar command, and boom… you just mistakenly executed this dangerous command because it was stored in your shell history.

I, at least, have been in that situation many times. Could we prevent specific commands from entering our mighty shell history to avoid these mistakes?

Philippe Martin shared that commands executed with a preceding space will not go into the session history. That sounds great!

# command goes into the history
$ delete everything

# command does not go into the history
$  delete everything

I tried it right away, but it didn't work. I'm a zsh user, and you have to enable it via a config in your .zshrc.

setopt histignorespace

In bash, the environment variable HISTCONTROL has to be set.

And there is one last thing to mention: immediately after command execution, every command will be accessible by pressing the UP arrow (this is a feature). Only when you execute another command, "preceding space commands" will be inaccessible.

But nevertheless, this little trick will prevent your future self from executing a dangerous command when pressing the UP arrow one time too much. 🎉

Preceeding space terminal demo

If you enjoyed this article...

Join 5.5k readers and learn something new every week with Web Weekly.

Web Weekly — Your friendly Web Dev newsletter
Reply to this post and share your thoughts via good old email.
Stefan standing in the park in front of a green background

About Stefan Judis

Frontend nerd with over ten years of experience, freelance dev, "Today I Learned" blogger, conference speaker, and Open Source maintainer.

Related Topics

Related Articles