This feature seems to exist also in bash. I discovered it accidentally while wondering why my history did not include my last commands (copy/pasted with a leading space).
Tip: you can prepend a space at the beginning of the command and it won't be saved in the history (though I agree that letting the command prompt for a password is better)
I used to do this, but with fish shell unless I have very similar commands, I just type part of the whole line to something I (personally) associate it with and that’s it, that’s the command.
I press a space before commands with sensitive information so they don’t stay in my history or throwaway commands that I’m 99% sure I won’t use again.
Complicated ones I create a function for it, but I no longer carry aliases on my shell configuration, and I prefer the legibility of my commands/history.
Heh. This is funny timing, as .bash_history made me feel like an idiot just yesterday morning.
I was sitting here between queries and I thought 'y'know, all I ever do with history is grep for things. I wonder what else I can do?' When I want to learn about something quick, I usually start with the built-in help; so I did
history --help
The built-in help for history, unfortunately, is woefully inadequate; this was the output:
history: usage: history [-c] [-d offset] [n] or history -awrn [filename] or history -ps arg [arg...]
I said to myself: "Hmm. Wonder what those do?" -- and in a moment of bash stupidity, I thought: "well, let's try it!"
history -c
There's no output from that command, but it didn't take long to figure out what it does. It deletes your entire bash history, clearing the history in RAM and emptying .bash_history.
Since I use history all the time, I panicked. Holy crap! What have I done? I spent a few minutes internalizing the obvious lesson that everybody knows - never run commands without knowing them first. Thankfully, though, I had another bash open at the time. And after I'd beaten myself up over it, I thought for a moment, jumped over to the other shell, and did:
cd && history | cut -c 8- >> .bash_history
... because (after all) the evil, evil history -c command doesn't kill the history from RAM in other shells.
Thank you!! Why on earth isn't that the default. It always seemed weird that with multiple bash windows open, the commands from most of them weren't added to the history.
Just put a space before you execute the command, then it doesn't get saved in history. Alternatively, delete it from your history file as the other commenter suggests.
You can view the history (with the line numbers) by typing 'history'. If you have the following set, you can avoid any command prefixed with a space ever getting in this list.
HISTCONTROL=ignorespace
I have trained myself, over many years, to hit space automatically before I type rm/reboot/shutdown etc. It's occasionally inconvenient if I need to rerun something but does mean I can't fat-finger anything destructive!
I've just tried and I can't reproduce. It might be because you're copying a leading space: if a command is prefixed by white space it doesn't get saved in the history.
With bash's HISTIGNORE, I can consciously prefix my command with a space to prevent it being added to history.
ls I usually don't care about, but there are directories I regularly cd to, so it would be nice to have those in history.
I can think of a neat heuristic, which is that I often cd to an absolute or home directory, so if the path starts with / or ~ I'll possibly want to cd there again in the future. Changing to a relative path on the other hand, I tend to do more rarely and while doing more ephemeral work.
reply