Script not running

I created history.sh script. It consist follow codes

history -c

i changed permission by using below command

chmod 777 history.sh

When i run ./history.sh
History still will be there. it does not erase history of commands.
Where is fault occure?

(Note: i am beginner in shellscript :unamused:)

@Buvanesh_Kumar,
why are you creating new thread?
This question originally is a part of Running script at before halt time

If you create unnecessary therad it will create problem.

1 Like

Ok sorry! i forgot about that. ok.
any solution for my problem?

Why are you trying to execute history -c.

Simply copy and paste the below contents to a file and save it.

#!/bin/bash
# Bash Shell store history in the file ~/.bashrc
rm -r ~/.bash_history
# Run the script and logout/reboot/shutdown to clear your history.

Also remember Bash has no built-in support for history command. Although you can enable history command mechanism.

Also note that you don’t need the above code to clear history everytime.

You can simply add the below line at the end of your ~/.bashrc file.
rm -r ~/.bashrc

This will force bash to delete the old .bashrc file and create a new automatically everytime.

1 Like

@Buvanesh_Kumar
If this helped you atleast you can tell me your problem was fixed.

@ravisaive
can we add something to each thread so that user can mark the thread as solved/fixed if the query is answered.

No! Still not working

  1. I proceeded as per your script. Script contains following lines

    #!/bin/bash
    rm -r ~/.bash_history
    then i changed it to be executable by using following command

    chmod +x foo.sh

Then executed by following command

./foo.sh

Still previous commands presents.

@Buvanesh_Kumar,
You didn’t paid attention to what i said. You have to logout/reboot/shutdown machine.
It is must/compulsory to logout/reboot/shutdown the machine.
Run the script again, then logout atleast. Login again and check.
Let me know what you are getting.

First, I tried to test the script is working or not, by executing the script. Its failure in testing. Then how I can use this script in other tasks. Now my aim is not erasing history in startup/restart/logout. Just run the script then it will erase history. That’s all
(sorry if my English is poor to understand)

This is not possible as far as i know. why?
You need to learn interpretation of history command with bash shell.
Also concentrate on how .bash_history file is written.
It is compulsory to logout/reboot/shutdown after running the above script.

Whats the error message you are getting when you run the script.

I have myself tested this and it works quiet well. Post what is the error you are getting when you run the script.

1 Like

Finally i find the problem.

Because i don’t have file called .bash_history in my home directory
Any solution?

Save the below script as history_clear.sh. Make it executable (chmod 755 history_clear.sh). Run it as ./history_clear.sh

#!/bin/bash

# Written by Avishek Kumar
# Time and date
TIME=$(date +"%Y-%m-%d_%H%M%S")

# Save current .bash_history file and throw output/error to /dev/null
cp ~/.bash_history ~/.bash_history_$TIME &>/dev/null

# Bash Shell store history in the file ~/.bashrc throw output/error to /dev/null
rm -r ~/.bash_history &>/dev/null

history -c && history -w

## Run the script and Reboot/shutdown to clear your history. ##

# Reboot Machine
sudo reboot

Let me know if your problem is solved or not?

1 Like

Superb :grinning:
Working fine
Thanks so much.
& i have one doubt.
TIME=$(date +"%Y-%m-%d_%H%M%S") - what it does?

Time is a variable which is used in file name to generate unique file name everytime based upon today’s date and time.

You may notice a new file created created as .bash_history_2015_… in your home folder. This file contains a backup of your history, in case you want to see it in future.

Each time you run the script/script run automatically, a file will be created automatically. To see all those files run

$ ls -l ~/.bash_history*

If you don’t want this feature comment out two lines from the above scripts. Just put a hash at the beginning of the below two lines simply as

# TIME=$(date +"%Y-%m-%d_%H%M%S")
# cp ~/.bash_history ~/.bash_history_$TIME &>/dev/null

2 Likes

Thank you so much for the useful information. Please recommend best shell script books to learn

Here are four free shell scripting ebooks that will help you to learn Linux scripting from scratch…

http://www.tecmint.com/free-linux-shell-scripting-books/

1 Like