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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With