๐Ÿ”’ Running Commands Without Leaving a Trace

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 HISTCONTROL environment variable is properly configured.


โš™๏ธ Understanding HISTCONTROL

The behavior depends on the value of the HISTCONTROL variable:

Value

Behavior

ignorespace

Ignores commands that start with a space

ignoredups

Ignores duplicate commands

ignoreboth

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:

  1. Run a command normally:

    whoami
    

    โ†’ Appears in history

  2. Run with leading space:

     whoami
    

    โ†’ Appears only if HISTCONTROL is not set or is not ignorespace


๐Ÿ› ๏ธ 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=ignoreboth is default

    • CentOS: HISTCONTROL=ignoredups is default

  • You can combine both features using:

HISTCONTROL=ignoreboth
Updated on