How to Execute a Command in Shell Without Saving it to History? [Linux Tips]

Updated on September 1, 2017

Well, you may call this as a Linux tip for the day. I’m going to share a tip that lets you to execute a command in shell prompt and does not save it in command history. Generally any command you execute in shell gets stored in a command history and the ‘history‘ command will display the list of commands that you had executed in the past. That’s quite useful. But what if you want to execute a command and then prevent it from storing in a command history?

Answer:

Executing any command starting with <space> will not be stored in command history.

For e.g.,

#<space> hostname

The above command will display the hostname of the machine, but the command itself is not stored in the command history.

HISTCONTROL command

But wait, you should control HISTORY (via HISTCONTROL environment variable). Check what is being set in HISTCONTROL as below:

#echo $HISTCONTROL

Now, set HISTCONTROL as below:

#export HISTCONTROL=ignorespace

Or you may add the above line to $HOME/.bashrc.

# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
 . /etc/bashrc
fi
# User specific aliases and functions
export HISTCONTROL=ignorespace

The above command controls the command history to ignore space. So whenever a command is executed with a space in the front, it will not be stored in history.

Was this article helpful?

Related Articles

Leave a Comment