Bash keeps two separate history lists:
- the in-memory list you use with ↑ / ↓,
history, !23, etc.
- the on-disk file (
$HISTFILE, default ~/.bash_history ) that is re-read at start-up and rewritten when the shell terminates.
If you simply unset HISTFILE before Bash has had a chance to read the file, the shell never loads the old commands, so the arrow keys appear to be “empty”.
The in-memory list still works, but it only contains the commands you’ve typed since you unset the variable, which can give the impression that history is broken.
Below are a few clean ways to keep a fully-functional in-memory history while making sure nothing is written back to disk when you leave the session.
──────────────────────────────────────────────────────────
Method 1 – Point HISTFILE at /dev/null
──────────────────────────────────────────────────────────
# put this at the very top of the session *after* .bashrc is finished
export HISTFILE=/dev/null
Bash will happily keep history in RAM, but when it tries to flush it, everything is discarded because /dev/null swallows the data.
Pros: trivial, works everywhere.
Cons: the history file on disk is not updated during the session, so new commands typed in this session will not be available to future sessions (that’s exactly what you asked for, but worth noting).
──────────────────────────────────────────────────────────
Method 2 – Keep the file, but make it zero-length
──────────────────────────────────────────────────────────
# keep 5 000 commands in memory
export HISTSIZE=5000
# but write 0 commands back to the file
export HISTFILESIZE=0
At exit Bash truncates the file to length 0.
During the session ↑ / ↓ work fine because HISTSIZE is not affected.
──────────────────────────────────────────────────────────
Method 3 – Load history, then drop the file handle
──────────────────────────────────────────────────────────
# load the file right now
history -r # (optional, if you’ve already got history loaded)
# stop any *future* writes
unset HISTFILE
Sequence matters:
- Let Bash start normally so it reads
~/.bash_history.
- Immediately after that,
unset HISTFILE.
• Old commands remain in memory (because they were already read).
• New commands you type are added to memory so ↑ / ↓ still work.
• When you exit, Bash has no file to write to, so nothing is saved.
Tip: if you already unset the variable too early, reload the file by hand:
HISTFILE=~/.bash_history # temporarily tell Bash where the file is
history -r # read it in
unset HISTFILE # and forget it again
──────────────────────────────────────────────────────────
Method 4 – Wipe just before exiting (trap)
──────────────────────────────────────────────────────────
# one-liner
trap 'history -c' EXIT
history -c clears the in-memory list right before the shell exits, so when Bash subsequently rewrites ~/.bash_history it has nothing to write.
Pros: you still update the file during the session with history -a, etc.
Cons: you need to remember/automate the trap each time.
──────────────────────────────────────────────────────────
Which one should I use?
──────────────────────────────────────────────────────────
• If you only occasionally need a “private” shell:
bash --init-file <(echo 'export HISTFILE=/dev/null')
• If you always want no-write behaviour: add one of the snippets (most commonly export HISTFILE=/dev/null) near the end of your ~/.bashrc.
That’s it—full arrow-key goodness, zero bytes written to ~/.bash_history 😎👍