This question already has an answer here:

I am writing an script to dump my history log into another file "myhistory.log", and then clear that history. I have written the following set of commands:

date >> myhistory.log

history >> myhistory.log

history -c

Everything goes well except that the previous history is not cleared. I have tried the following variations:

\history -c


CLRH="history -c"
exec $CLRH

What I am missing probably??

marked as duplicate by Radu Rădeanu, Warren Hill, terdon, BuZZ-dEE, Richard Mar 27 '14 at 18:52

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • It may seem a duplicate but my motive to ask the question was to learn how to specify the option while writing scripts. The history was just the example, as I stated in the title. – Ambuj Mani Tripathi Mar 27 '14 at 7:14
  • You complained the fact that history is not cleared. And I think that you are falling into XY problem. If so, you can edit your question and make it clear. – Radu Rădeanu Mar 27 '14 at 10:26
  • 1
    I read your original post and still don't understand what you are asking. What options are you talking about? Do you want to know why the history builtin acts differently in scripts? Please edit your question and clarify. – terdon Mar 27 '14 at 15:34
  • The problem was that the last statement in the script "history -c" was clearing the history temporarily so I used "> ~/.bash_history" after "history -w" command. Now it is correct. Thank you all for the support. – Ambuj Mani Tripathi Mar 28 '14 at 13:08

By default, for bash, the command history are stored in ~/.bash_history file.

As an alternative you can do this:

date >> ~/myhistory.log
cat ~/.bash_history >> ~/myhistory.log
echo -n "" > ~/.bash_history

This will append to ~/myhistory.log (if the file is already there, else create a new file and write to it) the date when the script was run, dump your history, and clear the ~/.bash_history file.

Not the answer you're looking for? Browse other questions tagged or ask your own question.