0

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

and

CLRH="history -c"
exec $CLRH

What I am missing probably??

Radu Rădeanu
  • 169,590
  • 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 07: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

1 Answers1

3

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

As an alternative you can do this:

#!/bin/bash
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.

rusty
  • 16,327