The Danger of the Bash History Log
By default, the Linux Bash shell records every single command you type into a hidden file located in your home directory (~/.bash_history). This is incredibly useful for pressing the up arrow to recall a complex string you typed ten minutes ago. However, it can also be a massive security vulnerability.
If you are connecting to a remote database server and need to pass a sensitive password directly in the command line string (e.g., mysql -u root -pMySuperSecretPassword), that plain-text password is immediately written to your history file. Anyone who gains access to your user account—or a system administrator analyzing the server—can simply open the file and read your password.
To prevent this, you can temporarily disable the history logging mechanism for your current terminal session.
Method 1: Disabling the History Feature Temporarily
The cleanest way to stop Bash from recording your commands is to use the set built-in command to disable the history option entirely.
Before you type your sensitive command, run:
set +o history
From this moment forward, absolutely nothing you type will be recorded in the history file or in the terminal’s memory buffer. You can run your database queries, execute API calls with secret keys, or manipulate sensitive user data.
Once you are finished, you must turn the history feature back on so you don’t lose the convenience of the up arrow for normal tasks:
set -o history
Method 2: Routing the History to /dev/null
Alternatively, if you want to ensure the history file is completely disabled for the entire duration of your session, you can overwrite the environment variable that tells Bash where to save the file.
export HISTFILE=/dev/null
By pointing the HISTFILE variable to /dev/null (the Linux black hole), Bash will attempt to save your commands, but the data will be instantly destroyed by the operating system. This restriction will automatically disappear the moment you close the terminal window or log out of the SSH session.
The Spacebar Trick (Per-Command Override)
If you only need to hide a single, specific command (and you don’t want to type two extra commands to toggle history off and on), many modern Linux distributions support the “ignorespace” feature.
If you type a space at the very beginning of your command, Bash will execute it but will intentionally exclude it from the history file.
mysql -u root -pSecretPassword
(Notice the leading space before the word mysql). You can verify this is enabled on your system by running echo $HISTCONTROL. If it outputs ignorespace or ignoreboth, the spacebar trick will work perfectly.