Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use 'history-c' command in a bash script? [closed]

Tags:

linux

shell

As we know, 'history' command displays the command line history of Linux server and 'history -c' is the command to clear/delete this command line history.

I have to trigger this command through my bash script. Script is as follows,

#! /bin/bash
var=`history -c`
if [ $? -eq 0 ]
then
echo "cleared"
echo $var
fi

Output is as follows:

  cleared

Though its printing "cleared" as the output, history-c is not deleting the history.

It would be great if you can guide/suggest on how i can achieve this, i.e using "history-c" command in my bahs script to delete command line history.Or is there any other way in which i can delete command line history through my bash script.

Thanks & Regards, Navya

like image 407
NAVYA L Avatar asked Dec 07 '15 04:12

NAVYA L


1 Answers

history -c clears the history for the current shell, and does not delete ~/.bash_history.

But when you run a script the current shell creates a new shell to run the script in and exits that shell when the script is done.

Instead, to execute a script in the current shell you have to source the script. If the name of your script is foo.sh, try running . ./foo.sh

But in either case, the script that you've written does not execute the command. Modify it to something like this:

#! /bin/bash
history -c
if [ $? -eq 0 ]
then
echo "cleared"
fi
like image 69
Nikhil Saldanha Avatar answered Nov 15 '22 10:11

Nikhil Saldanha