0

I'm creating a script to run in the start up that says:

_cleanthehistory ()
{
/bin/echo "cleaning the history"
history -c
}
_cleanthehistory

The deal here is that this does not work from the script, but if I run it from the command line works. Any ideas? Thanks in advance. Pablo.

5
  • Is this the whole script or did you put that somewhere (e.g. in the bashrc)? If so, please post the whole script. If not, why did you write it inside a function instead of a plain /bin/echo "cleaning the history"; history -c. Also I think you're missing a semicolon in the version posted here.
    – con-f-use
    Mar 30, 2012 at 18:02
  • Hi, yes this is a section of the script.
    – pma083
    Mar 30, 2012 at 18:06
  • As I said, there is a semicolon missing between the echo and the history command. Also if you do that from within a script you do it from the local scope of the script. Very much the same if you change a variable within a script it is not changed outside (in the terminal you ran the script in).
    – con-f-use
    Mar 30, 2012 at 18:12
  • @con-f-use there are actually newlines there, they were just turned into spaces due to using the wrong formatting (for this site).
    – geirha
    Mar 30, 2012 at 18:16
  • @geirha is right is because of the format of this site.
    – pma083
    Mar 30, 2012 at 18:21

2 Answers 2

1

As I said in my comment history -c just deletes the history of the current session, i.e. only the commands your script ran. To clear all data including old sessions you'll have to do something like

rm -f $HOME/.bash_history
3
  • thanks for your reply, i'll try deleting the .bash_history file.
    – pma083
    Mar 30, 2012 at 18:26
  • It didn't worked. :(
    – pma083
    Mar 30, 2012 at 18:30
  • cat /dev/null > ~/.bash_history :) Thanks to all for the help!.
    – pma083
    Mar 30, 2012 at 18:40
0

History is only enabled for interactive shells, not scripts. So in a script, history -c won't do much good. If you just want to empty your user's bash history during boot, as /tmp is emptied during boot, just truncate or delete ~/.bash_history.

echo "cleaning the history"
> /home/yourusername/.bash_history

This uses shell redirection without a command. It makes the shell just open the file for writing (truncating it if it already exists), then close it without writing anything to it (since there's no command to write anything to it).

On a side note, don't use absolute paths to commands in scripts. E.g. use echo, not /bin/echo.

update: Or if you don't want to save bash history at all, you could put something like HISTFILE=/dev/null in your ~/.bashrc.

2
  • Thanks @geirha I've tried that but didn't worked also, is really weird, in a debian server works like a charm, but not in the ubuntu .
    – pma083
    Mar 30, 2012 at 18:34
  • @pma083 How exactly are you running it at start up?
    – geirha
    Mar 30, 2012 at 18:37

You must log in to answer this question.

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