Sometimes, you may want to execute commands without them being saved in the Bash history. This is useful for privacy, security, or simply avoiding clutter in the history log.
๐ก Problem: History Logs Everything
By default, Bash logs every command you run to:
-
Memory (during session)
-
~/.bash_history(on logout)
Even if you manually edit or clear the history, it can raise suspicion or still leave traces.
โ Solution 1: Prepend a Space
-
Prefix your command with a space:
-
wget https://www.example.com/secretfile.jpg -
This works only if the
HISTCONTROLenvironment variable is properly configured.
โ๏ธ Understanding HISTCONTROL
The behavior depends on the value of the HISTCONTROL variable:
Value | Behavior |
|---|---|
| Ignores commands that start with a space |
| Ignores duplicate commands |
| Combines both behaviors |
(empty) | All commands are recorded |
echo $HISTCONTROL # View current value
Example:
HISTCONTROL=ignorespace # Ignores commands that start with a space
HISTCONTROL=ignoredups # Ignores duplicate commands
HISTCONTROL=ignoreboth # Ignores both duplicates and spaced commands
๐งช Testing:
-
Run a command normally:
whoamiโ Appears in
history -
Run with leading space:
whoamiโ Appears only if
HISTCONTROLis not set or is notignorespace
๐ ๏ธ Make HISTCONTROL Persistent
Session-only changes will be lost on logout or reboot. To persist them:
echo 'HISTCONTROL=ignoreboth' >> ~/.bashrc
Then reload your shell:
source ~/.bashrc
โ ๏ธ Important Notes
-
Behavior may vary across distributions:
-
Ubuntu:
HISTCONTROL=ignorebothis default -
CentOS:
HISTCONTROL=ignoredupsis default
-
-
You can combine both features using:
HISTCONTROL=ignoreboth