Fish Shell, how to avoid recording commands to history
By Flavio Copes
Learn how to run commands in Fish shell without saving them to history by starting a private session with fish --private, then exiting when you are done.
To run commands in Fish without saving them to history, start a private session with fish --private. Everything you type in that session stays out of the history file. When you exit, it’s gone.
Sometimes you want to run some commands in the shell, but you don’t want them to be stored in the shell history.
Maybe the command contains an API key or a password. Maybe you’re testing something on a shared machine. Whatever the reason, you don’t want it sitting in a plain text file on disk forever.
How to start a private session
With Fish Shell, my default shell, start a new shell with:
fish --private
You’re now in a nested shell. Fish will not read your old history, and it will not store anything new. Run whatever you need, then leave with:
exit
You’re back in your normal shell, and nothing you typed in the private session was recorded.
If you’re ever unsure whether you’re in a private session, check the $fish_private_mode variable. Fish sets it to 1 in private mode:
echo $fish_private_mode
In a normal session, it prints an empty line.
What if it’s just one command?
Starting a whole new shell for a single command feels like overkill. There’s a shortcut: prefix the command with a space.
curl -H "Authorization: Bearer sk_live_abc123" https://api.stripe.com/v1/charges
Fish will not store that line in the history file. You can still recall it with the up arrow until you run the next command, which is handy for fixing typos, but it never touches the disk.
One thing to watch out for
Private mode only protects the Fish history. The programs you run can keep their own records.
If you open psql inside a private session and run queries, psql still writes them to its own ~/.psql_history file. Same idea for language REPLs and other interactive tools. Fish can’t do anything about that, so check the tool’s own history settings if it matters.
By default, Fish keeps its history in ~/.local/share/fish/fish_history. If you already ran something sensitive, you can delete individual entries with history delete --contains "sk_live".
I wrote an article on the Fish Shell basics if you’re interested in trying it. I highly recommend Fish over any other shell, due to its ease and “it just works” philosophy.
Related posts about cli: