1

I am creating shelll script that is erase history of commands in the terminal. But the script is not erase the history of commands in the terminal. I follow below procedure 1. My script contains following lines

#!/bin/bash
history -c

2. I changed file to be executable

chmod +x foo.sh

3. I run the script

./foo.sh

but the script is not working & not erasing my history of commands. Why?

Update: I alternatively write a script to clear the history. still same problem occurs. My alternative script is,

#!/bin/bash
# Bash Shell store history in the file ~/.bashrc
rm -r ~/.bash_history

This is also not clear history of commands.

2 Answers 2

3
  • rm -f ~/.bash_history (don't use -r!) will remove the history log, which in the default configuration of most distros is read when you start a new shell. Doing this will therefore only affect shells that you start after running this command. You can replace your current shell with another one by running exec "$SHELL" to avoid creating a new terminal window.
  • 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. In your case, try running . foo.sh.
1

I you want to "clear our traces" after you exit the shell you might want to run these commands from your logout script, that is the ~/.bash_logout file.

Maybe you do not want at all your history of commands to be saved, in this case you can set HISTFILESIZE=0 (in your ~/.profile file or in the shell you are using).

See also: http://ubuntuforums.org/showthread.php?t=920530

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .