A preceding space prevents dangerous commands from going into history
- Published at
- Updated at
- Reading time
- 1min
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
.
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. 🎉
Join 5.5k readers and learn something new every week with Web Weekly.