Execute command without keeping it in history

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Execute command without keeping it in history



I want to execute some commands but don't want to store them in the command history. So that nobody will be able to search it in the .bash_history file.


.bash_history



Is there any way how to execute bash commands this way?





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!
– Andrew Theken
Aug 29 '14 at 13:39




13 Answers
13



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.


$HISTCONTROL



Check that the following command returns ignorespace or
ignoreboth


ignorespace


ignoreboth


#> echo $HISTCONTROL



To add the environment variable if missing, the following line can be
added to the bash profile. E.g. %HOME/.bashrc


%HOME/.bashrc


export HISTCONTROL=ignorespace



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


$HISTFILE





This doesn't work unless ignorespace is included in $HISTCONTROL - although this is probably the default on most systems.
– pgl
Dec 12 '11 at 10:58





Correct. For more details the OP should have a look at HISTCONTROL and HISTIGNORE in the bash man page.
– u-punkt
Dec 12 '11 at 11:02





thegeekstuff.com/2008/08/… - more bash history tips are available here including this method of hiding commands from history
– user379997
Dec 12 '11 at 14:38




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 sysadmin's hard work please ;)



[UPDATE] @Doodad's solution is more elegant, simply unset the variable: unset HISTFILE (thanks!)


unset HISTFILE





This will prevent all commands from being logged. It's not what the questioner wants.
– Noufal Ibrahim
Dec 12 '11 at 11:07





This caused problems for me related to /dev/null permissions being set to 0600 on logout. Instead I used unset HISTFILE.
– Doodad
Oct 6 '14 at 16:53



/dev/null


unset HISTFILE


echo "discreet";history -d $(history 1)





Ooh, per-command control? Nice.
– Kyle Strand
Nov 5 '15 at 1:12



An extension of @John Doe & @user3270492'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.



https://superuser.com/a/529193



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)


/bin/dash


/bin/sash


/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


sh


batch



You also can 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
– sobi3ch
Jan 12 '17 at 17:54


alias forget="history -d $(history | sed -n '$s/s*([0-9]*)s*.*$/1/p')"


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 //
– sobi3ch
Jan 12 '17 at 18:05



$s


function forget() // put code here //



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 histor file) will be shown.


/dev/null



Also, this is non-permanent.





This was already answer many times. What would you say your answer adds to this thread (except for useless noise)?
– gniourf_gniourf
Jan 2 at 10:07





It wasn't put together in one answer, so as to not have the command itself show up in the history and not have anything show up that's entered thereafter.
– Christian
Jan 2 at 18:28





But now this thread is a real mess! in this case, especially for such an old question, I believe it's better that you edit already existing good answers, and/or leave a comment…
– gniourf_gniourf
Jan 2 at 18:32



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), then unset HISTFILE, then edit ~/.bash_history (or whatever HISTFILE was - of course it's now unset so you can't read it).


unset HISTFILE


echo $HISTFILE


~/.bash_history


unset HISTFILE


~/.bash_history


HISTFILE


unset


$ 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
Mar 1 '17 at 13:46





@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).
– drkvogel
Mar 2 '17 at 20:49





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 :)
– Krystian
Mar 2 '17 at 20:57



unset HISTFILE; echo TEST; history


echo TEST





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! ;)
– drkvogel
Mar 3 '17 at 21:38


unset HISTFILE



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:


/dev/null


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).


ignorespace


$HISTCONTROL


ignorespace





This will prevent all commands from being logged. It's not what the questioner wants.
– Noufal Ibrahim
Dec 12 '11 at 11:07





Well, I kind of assumed that the questioner would reset the values after doing whatever it was he wanted that shouldn't be logged.
– pgl
Dec 12 '11 at 12:04



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



If you are using 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:


.zshrc


# 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."'



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



history -d $((HISTCMD-1)) &&



You just need to run:
$ set +o history


$ set +o history



To see more, run:
$ man set


$ man set






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard