1

This question already has an answer here:

I want to make a bash script that clears the history i.e. works similarly as the command

history -c

So I started with the following code:-

#!/bin/bash
history     #displaying history
history -c  #clearing history

None of them worked. On some searching around, I came to know that bash disables history in non-interactive shells by default, but we can turn it on. So after editing I tried the following code :-

#!/bin/bash
HISTFILE=~/.bash_history   # Or wherever you bash history file lives
set -o history             # enable history
history
history -c

It shows the output :-

[root@localhost lib]# bash a.sh
1  history
[root@localhost lib]#

And besides that the hisory -c command did not work. Because I am still getting the history of commands when I type history. This means both history and history -c didnt work inside the bash script.

How should we use it then?

EDIT 1- I want to delete the history of the current session, it must be stored somewhere. I have tried using commands like the following but to no effect:-

cat /dev/null > ~/.bash_history && history -c && exit


cat /dev/null > ~/.bash_history

P.S.-This is not a duplicate question. Please try to understand the difference before marking it as duplicate. I want to clear the history of the current session through a script . I don't care if it is written back or whatever. The other question is about to permanently delete the history. It has nothing to do with the script or through other terminal.

marked as duplicate by Sylvain Pineau, Eliah Kagan, Eric Carvalho, karel, Pandya Sep 18 '14 at 17:21

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.

  • @Sylvain : This is not a duplicate question. Please try to understand the difference before marking it as duplicate. I want to clear the history of the current session through a script . I don't care if it is written back or whatever. The other question is about to permanently delete the history. It has nothing to do with the script or through other terminal. – Dhruv Chandhok Sep 23 '14 at 19:49
1

You can't delete history like this as it will delete the history of current session.

If you wan to clear your history using script use following command in your script

> ~/.bash_history

It is enough to clear all your bash history .

  • I tried using this command inside my script. When I executed the bash file and ran the history command, the entire history was shown. I want to delete the history of the current session, it must be stored some where. – Dhruv Chandhok Sep 15 '14 at 11:16
  • Ok, let me test it.. – g_p Sep 15 '14 at 11:34
  • I think you can't clear it from your current session as history of current session is stored in the memory which is written back when you logout. You can add above to your .bash_logout so that you can clear you history when you logout. – g_p Sep 15 '14 at 12:10
0

I think that

history -c 

will only clear the history inside the seesion associated with the script This example:

#!/bin/bash
HISTFILE=~/.bash_history   # Or wherever you bash history file lives
set -o history             # enable history
uname -a > /dev/null
history
history -c
history

shows the output:

....
500  history
501  uname -a > /dev/null
502  history
4  history

which is what one expects.

If you want to erase the history, try

> $HISTFILE
  • 1
    You assume the $HISTFILE can be removed without consequences? I would use an instruction to clear the file from content. – Rinzwind Sep 15 '14 at 10:33
  • 1
    I'm reasonably sure that it can be. But, yes, simply clearing also works. I have edited – Gremlin Sep 15 '14 at 10:58
  • I want to clear history associated wwith the terminal. How to do that? – Dhruv Chandhok Sep 16 '14 at 12:20

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