clear history from shell

Karl Auer kauer at biplane.com.au
Mon May 16 13:57:55 UTC 2016


On Mon, 2016-05-16 at 13:18 +0100, Grizzly wrote:
> I have a simple script,
> 
> #!/bin/sh
> history -c && history -w && exit
> 
> each part was tested manually and the whole line has also been 
> tested, but once I run the script it returns
> 
> "3: Scripts/myclean.sh: history: not found"

You changed the shell from bash (or dash) to sh in the first line of
your script. Change it to /bin/bash and your script will run.

However, it will not work as you expect - it will clear the history in
a new shell, not your interactive shell. And it will not exit your
shell.

After fixing the shell specifier, you could run the script like this
and it would sort of work:

. Scripts/myclean.sh

or

source Scripts/myclean.sh

That's called "sourcing" the script, and the commands are executed in
your running shell, not a new one.

You will find that these will still not exit the running shell though.
I'm not sure, but I don't think these built-ins have return codes, so
probably only the first element in your sequence is actually running.

To get everything you want, an alias would probably be best:

alias myclean="history -c ; history -w ; exit"

I think "history -w" is unnecessary - the current (empty) list will be
written to the history file on exiting the shell anyway.

BTW you can do this any time to turn off the history from this point:

   set +o history

It stops more history being remembered, but history up to this point
will still be remembered.

> any pointers to what stupid error I made please

Ignorance is not stupidity.

Regards, K.

-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Karl Auer (kauer at biplane.com.au)
http://www.biplane.com.au/kauer
http://twitter.com/kauer389

GPG fingerprint: E00D 64ED 9C6A 8605 21E0 0ED0 EE64 2BEE CBCB C38B
Old fingerprint: 3C41 82BE A9E7 99A1 B931 5AE7 7638 0147 2C3C 2AC4







More information about the ubuntu-users mailing list