2

Clarification: Am not looking to destroy history, am looking for means to periodically purge it via launchd for security purposes

Have been using cat /dev/null > ~/.bash_history to clear bash history as have seen in many *nix conversations as the most effective way to do so.

So I put it into a shell script that runs in launchd, yet after some time I've come to realize though it runs it's not clearing history.

Is it possible to clear bash history this way via bash script or can it only be done manually as a user? Perhaps I need to run the command directly in the .plist and not via a bash script?

Note: I have other scripts running correctly via launchd so question is strictly about being able to clear history via an agent

Thank you

Update: Found this cool site that helps generate launchd scripts if this helps anyone. Cannot vouch for it but it's much easier than hacking XML. It seems to use sh -c for all commands though, so not sure why that is.

launchd plist generator http://launched.zerowidth.com/

2
  • 2
    Please add the plist and the script to the question.
    – nohillside
    Oct 23 '20 at 5:27
  • FWIW: If you're keen on using launchd, you should take a look at LaunchControl. It's not free, but it's brilliant, and well worth the modest price. After using it for a few weeks, you'll realize that launchd actually is useful!
    – Seamus
    Oct 23 '20 at 17:21
5

Your bash history consists of two "histories", not one. One of these histories is stored in a file - its size/depth is governed by the parameter HISTFILESIZE. The other history is cached in memory - its size is governed by the parameter HISTSIZE.

You can check the sizes/depth of your histories:

$ echo $HISTFILESIZE 
$ echo $HISTSIZE

The reason for two histories is that one is associated with each shell session (HISTSIZE), while the other history is a file ~/.bash_history that is "permanent", and eventually receives the session histories when they overrun their max depth, or when the session is terminated. That's a bit complicated to explain, and the picture below may help make the relationship between these two histories clear.

But back to your objective:

Rather than taking steps to delete your history, you could simply set HISTSIZE and HISTFILESIZE to zero in ~/.bashrc:

HISTSIZE=0
HISTFILESIZE=0

If that's not what you want, you can certainly delete the history file periodically, but know that doesn't delete the session history. Deleting your history file by building a .plist file isn't necessary. The history command is the tool to use for this job (see history --help for details). The following command will delete one session history:

$ history -cw

Deleting the contents from the history file (~/.bash_history) is done conventionally:

$ > ~/.bash_history

You could put one or both of these commands in a crontab, but you'll need to ensure the default shell is bash as history is a built-in.

For some related background on the subject of shell history, you could read this related Q&A. It pertains to zsh - not bash, but the principles are the same.

enter image description here

10
  • crontab has been deprecated in Mac OS though was using it before and is why am jiggling around with launchd again now. Im not looking to wipe history as I definitely use it! It's wanting to purge it periodically -- is there any code that can automatically run when I close a terminal session?
    – iGuest
    Oct 23 '20 at 15:18
  • Ok.. so I see now, if I just set HISTFILESIZE=0 the file is not generated and I'll still get terminal session history!?
    – iGuest
    Oct 23 '20 at 15:25
  • Ok wow, thanks you've pointed me in the right direction: "When one exits the bash, if there are more than $HISTSIZE number of commands which have been executed in the single bash session, the contents of $HISTFILE will be replaced by the $HISTSIZE number of commands. If there are less than or equal to $HISTSIZE number of commands in the bash session, these commands will be appended to $HISTFILE as long as $HISTFILESIZE permits." -- thegeekdiary.com/…
    – iGuest
    Oct 23 '20 at 16:45
  • 1
    "if I just set HISTFILESIZE=0 the file is not generated and I'll still get terminal session history" Yes, that's the way it works.
    – Seamus
    Oct 23 '20 at 16:57
  • 1
    well developer.apple.com/library/archive/documentation/MacOSX/… says "Note: Although it is still supported, cron is not a recommended solution. It has been deprecated in favor of launchd." -- but excellent point it's part of the *nix OS so they can't truly toss it. and yes the plist concept is obnoxious. Thank god we have JSON and HTML5 for the web
    – iGuest
    Oct 23 '20 at 17:18
5

It seems you've come up with a very convoluted solution to the problem of not wanting a bash_history.

I would advise that instead of periodically clearing the history from launchd, you should simply disable history recording:

echo 'set +o history' >> ~/.bashrc

Close the terminal tab and open a new one - and your history will no longer be recorded.

3
  • 1
    And probably run rm -f ~/.bash_history once to remove the old one.
    – nohillside
    Oct 23 '20 at 8:52
  • no these solutions don't work because i DO want to keep history but also want it to clear out for security purposes. Removing the file doesn't allow you to generate new history -- a more elegant solution to your rm is history -wc to clear and overwrite the history file. I am looking for a way to automate this sort of action.
    – iGuest
    Oct 23 '20 at 15:16
  • ok thanks, i didn't realize the relationship between the history file and session history. so thats the key as per the other comments here. Thank you
    – iGuest
    Oct 23 '20 at 16:59

You must log in to answer this question.

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