287

When doing software development, there is often need to include confidential information in command line commands. Typical example is setting credentials for deploying the project to a server as environment variables.

How can I execute some commands, when I don't want to store them in the command history?

Nobody must be able to search them in the .bash_history file. Is there a way how to execute Bash commands this way?

1
  • 20
    This question should probably be moved to ServerFault as this is not "programming" as much as it is "server admin" - but thanks for the question/answer, exactly what I needed! Commented Aug 29, 2014 at 13:39

13 Answers 13

401

Start your command with a space and it won't be included in the history.

Be aware that this does require the environment variable $HISTCONTROL to be set.

  • Check that the following command returns ignorespace or ignoreboth:

     echo $HISTCONTROL
    
  • To add the environment variable if missing, the following line can be added to the Bash profile. E.g., to file %HOME/.bashrc.

     export HISTCONTROL=ignorespace
    

After sourcing the profile again, space-prefixed commands will not be written to $HISTFILE.

Sign up to request clarification or add additional context in comments.

This doesn't work unless ignorespace is included in $HISTCONTROL - although this is probably the default on most systems.
Correct. For more details the OP should have a look at HISTCONTROL and HISTIGNORE in the bash man page.
thegeekstuff.com/2008/08/… - more bash history tips are available here including this method of hiding commands from history
If you put space before the command, that won't save the command in history for some OS.
74

In any given Bash session, set the history file to /dev/null by typing:

export HISTFILE=/dev/null

Note that, as pointed out in the comments, this will not write any commands in that session to the history!

Just don't mess with your system administrator's hard work, please ;)

Doodad's solution is more elegant. Simply unset the variable: unset HISTFILE (thanks!)

This will prevent all commands from being logged. It's not what the questioner wants.
This caused problems for me related to /dev/null permissions being set to 0600 on logout. Instead I used unset HISTFILE.
Is some sort of restart required? I have used unset HISTFILE, it was there before, now it is gone, but my command still got logged.
I learned that using unset HISTFILE will stop history from writing into the persistent file. However it will keep the commands in memory. Probably until you log off.
48
echo "discreet";history -d $(history 1)

Ooh, per-command control? Nice.
This doesn't work in zsh. It outputs fc: too many arguments and does store the command in the history.
33

An extension of John Doe's and Cédric ROYER's answer. But, this seems to work for me.

<your_secret_command>; history -d $((HISTCMD-1))

You should not see the entry of the command in your history.

Here's the explanation...

The 'history -d' deletes the mentioned entry from the history.

The HISTCMD stores the command_number of the one to be executed next. So, (HISTCMD-1) refers to the last executed command.

Remove a certain line from Bash history file

Comments

20

If you are using Z shell (zsh) you can run:

setopt histignorespace

After this is set, each command starting with a space will be excluded from history.

You can use aliases in .zshrc to turn this on/off:

# Toggle ignore-space. Useful when entering passwords.
alias history-ignore-space-on='\
setopt hist_ignore_space;\
echo "Commands starting with space are now EXCLUDED from history."'

alias history-ignore-space-off='\
unsetopt hist_ignore_space;\
echo "Commands starting with space are now ADDED to history."'

Comments

12

You might consider using a shell without history, like perhaps

/bin/sh << END
   your commands without history
END

(perhaps /bin/dash or /bin/sash could be more appropriate than /bin/sh)

Or even better, use the batch utility, e.g.,

batch << EOB
   your commands
EOB

The history would then contain sh or batch which is not very meaningful.

Comments

11

You can start your session with

export HISTFILE=/dev/null ;history -d $(history 1)

then proceed with your sneaky doings. Setting the histfile to /dev/null will be logged to the history file, yet this entry will be readily deleted and no traces (at least in the history file) will be shown.

Also, this is non-permanent.

Comments

7

As mentioned by Doodad in comments, unset HISTFILE does this nicely, but in case you also want to also delete some history, do echo $HISTFILE to get the history file location (usually ~/.bash_history), unset HISTFILE, and edit ~/.bash_history (or whatever HISTFILE was - of course it's now unset so you can't read it).

$ echo $HISTFILE       # E.g. ~/.bash_history
$ unset HISTFILE
$ vi ~/.bash_history   # Or your preferred editor

Then you've edited your history, and the fact that you edited it!

Tried this on: GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin15) and it does not work. The commands I used after unset HISTORY were stored in ~/.bash_history
@Krystian My mistake - I had written "unset HISTORY" when it should be "unset HISTFILE" - corrected. Does it work now? It works on GNU bash, version 4.3.30(1)-release (x86_64-pc-linux-gnu).
well I am not sure. Simple test: unset HISTFILE; echo TEST; history will give you the command echo TEST as the previous to last. Although after terminating the session and connecting again, history does not have this entry indeed. So I would say it works :)
unset HISTFILE does not seem to stop commands being recorded in the history in memory, but it does stop them being recorded in whatever file $HISTFILE was. So I guess you still have your local history in memory, but it is not being recorded in a file anymore, which is what you might want! ;)
4

You can also use the following command:

echo toto; history -d $(history | sed -n '$s/\s*\([0-9]*\)\s*.*$/\1/p')

I think it's a very portable command.

I've created alias from this: alias forget="history -d $(history | sed -n '$s/\s*\([0-9]*\)\s*.*$/\1/p')" and now You can do it something like this: mysql -uroot -pSecretPassword ; forget
Sorry, but I realise You need to create function instead of alias because of quotation marks change meaning of $s and other things in sed. function forget() { // put code here // }
3

There are several ways you can achieve this. This sets the size of the history file to 0:

export HISTFILESIZE=0

This sets the history file to /dev/null, effectively disabling it:

export HISTFILE=/dev/null

For individual commands, you can prefix the command with a space and it won't be saved in the history file. Note that this requires you have the ignorespace value included in the $HISTCONTROL environment variable (man bash and search for ignorespace for more details).

This will prevent all commands from being logged. It's not what the questioner wants.
Well, I kind of assumed that the questioner would reset the values after doing whatever it was he wanted that shouldn't be logged.
3

You just need to run:
$ set +o history

To see more, run:
$ man set

This disables history for all future commands, not for an individual command. Most people who come here should look at the accepted answer instead.
This is valid, or okay but incomplete. As long as we add the reverse command set -x history afterward, it works.
I'm reading the manual and it would seem that set -o history turns the history back on. Are you sure it's set -x history?
A subshell is a convenient way to solve this: ( set +o history; do a thing ) and you don't have to remember to turn it back on because it's already back on when the subshell exits.
2

This command might come in handy. This will not record the command that is executed

history -d $((HISTCMD-1)) && <Your Command Here>

Comments

1

This is handy if you want to erase all the history, including the fact that you erased all the history!

rm .bash_history;export HISTFILE=/dev/null;exit

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.