Skip to content
shellhacks.dev
Security

Turn Off Bash History for the Current Session

Stop a Bash session from saving history with unset HISTFILE. The commands stay in memory until the shell exits, but nothing is saved to the file.

1 min read
Summarize in AI

A session will handle sensitive commands, and you want none of it saved when you leave.

Shell
export HISTFILE=$PWD/hist
bash -i > /dev/null 2>&1 <<'EOF'
echo before
unset HISTFILE
echo after
EOF
ls hist 2> /dev/null || echo "no history file was written"
bash -i > /dev/null 2>&1 <<'EOF'
echo kept
EOF
cat hist
Text
no history file was written
echo kept

Bash writes the in-memory history to the file named by HISTFILE when an interactive shell exits. Unsetting the variable first means there is no file to write: in the run echo before and echo after produced no history file at all, and a second session without the unset saved echo kept. The session's history is still available with the history command while it lasts. See Bash History Facilities.

It affects the current shell only, and commands from before the unset are not saved either, because writing happens at exit. It does not touch the terminal's scrollback or logs kept by sudo, auditd or a remote shell. To clear what is in memory instead, run history -c.

Share

More snippets

Get new posts by email

One email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.