If the environment variable HISTCONTROL includes ignorespace or ignoreboth, any command you type with a leading space will not be saved to the history.
export HISTCONTROL=ignoreboth
Then:
ls -l # saved to history
ls -l # not saved to history (note the leading space)
2. Use HISTCONTROL=ignorespace or ignoreboth
Make sure your HISTCONTROL variable is set properly:
export HISTCONTROL=ignoreboth
ignorespace: commands starting with a space are not saved.
ignoredups: duplicate commands are not saved.
ignoreboth: combines both.
3. Use unset HISTFILE temporarily
If you want to run commands without saving any commands during a session:
unset HISTFILE
This disables writing to the history file for the current session.
4. Use history -d to delete the last command
After running a command, you can delete it from history:
some_command
history -d $(history 1 | awk '{print $1}')
5. Use bash -c with HISTFILE=/dev/null
Run a command in a subshell with no history file:
HISTFILE=/dev/null bash -c 'your_command_here'
6. Disable history for a single command with set +o history
Turn off history temporarily:
set +o history
your_command
set -o history
Summary
Easiest: Start command with a space (if HISTCONTROL is set to ignoreboth or ignorespace).
For one-off:HISTFILE=/dev/null bash -c 'command'
For session:unset HISTFILE
For after-the-fact:history -d <line_number>
If you want, I can help you set this up in your .bashrc or explain any method in more detail!
User
how to check current HISTCONTROL value?
Assistant
To check the current value of the HISTCONTROL environment variable in Bash, you can use the echo command:
echo "$HISTCONTROL"
This will print the current setting of HISTCONTROL. If it’s empty, it means no special history control options are set.
Example:
$ echo "$HISTCONTROL"
ignoreboth
If nothing prints, it means HISTCONTROL is unset or empty.
You can also use:
env | grep HISTCONTROL
to see if it’s set in the environment.
User
echo $HISTCONTROL shown the value, but 'env' or 'prinenv' doesnt'. Why is it so?
Assistant
Great question!
The reason you see a value with echo $HISTCONTROL but not with env or printenv is because:
HISTCONTROL is a shell variable, not necessarily an environment variable.
Shell variables exist only within the shell session.
Environment variables are exported to child processes and visible to commands like env or printenv.
What’s happening?
When you do echo $HISTCONTROL, the shell expands the shell variable HISTCONTROL.
When you run env or printenv, they only show environment variables that have been exported.
If HISTCONTROL is not exported, it won’t appear in env or printenv.
How to check if HISTCONTROL is exported?
Run:
export -p | grep HISTCONTROL
If nothing shows up, it means HISTCONTROL is not exported.
How to export it (if you want)?
export HISTCONTROL
After this, env | grep HISTCONTROL will show it.
Summary
HISTCONTROL is usually a shell variable controlling Bash behavior.
It’s not exported by default, so it’s visible inside the shell but not to child processes.
echo $HISTCONTROL reads the shell variable.
env and printenv show only exported environment variables.
Let me know if you want to see how to export or manipulate these variables!