0

I want to clear my shell history on every shutdown/logout.

I know how to clear the history manually, I know how to clear the history automatically, when closing the session/terminal emulator. However, this is not the intended behaviour.

I am using zsh on manjaro with wayland and sway.

I have tried using .zlogout. However, this did not work, as far as I understand, because it is ment for login shells (I don't know what makes a login shell).

2
  • History is saved only if you ask the shell to save it (by setting $HISTFILE and $SAVEHIST in ~/.zshrc). Why not just not do that history saving. Commented Aug 17, 2023 at 3:18
  • @StéphaneChazelas because I want to save history during one session (I often use similar commands again and again), but not over the course of several sessions. Commented Aug 25, 2023 at 10:34

2 Answers 2

2

If you happen to use systemd (which manjaro should AFAIK), one option would be a simple service that just succeeds and has the history-deletion executed when it stops. Then make it depend on default.target:

[Unit] Description=Delete zsh-history on logout/shutdown [Service] Type=oneshot RemainAfterExit=true ExecStart=/bin/true ExecStop=/home/grbll/bin/zsh-remover.sh [Install] WantedBy=default.target 

User services go e.g. in ~/.config/systemd/user/zsh-history-wipe.service

For enabling it permanently and for activating it for your current session once run

systemctl --user enable zsh-history-wipe systemctl --user start zsh-history-wipe 

As all services are stopped with the last logout or on shutdown, the ExecStop-command will be run in the end. Obviously does not work for e.g. power outages.

Note that lingering must be disabled for the user!

3
  • Thank you for this solution, I also learned how to do similar things in the future. I had to replace ExecStop=/home/grbll/bin/zsh-remover.sh by ExecStop=/bin/zsh /home/grbll/bin/zsh-remover.sh. Even though I used a bang tag in zsh-remover.sh. Commented Aug 25, 2023 at 14:55
  • @grbll did you set the executable bit on the script? (chmod u+x /home/grbll/bin/zsh-remover.sh) Commented Sep 4, 2023 at 6:27
  • yes, it is -rwxr-xr-x owned by $USR:$USR. Commented Sep 4, 2023 at 10:29
-1

You can create a shell script that clears your history and place it in a location where it will be executed on logout or shutdown. FOllow these steps:

  1. The file may look like this:
#!/bin/zsh history -c history -w 
  1. Make the file executable: chmod +x script.sh
  2. Configure your Sway (~/.config/sway/config) adding this command to the end of the config file: exec /path/to/the/script.sh

And that's it!

1
  • 1
    "I know how to clear the history manually," you're solving the problem that grbll already knows how to solve. What grbll wants to know is where to put this so that it gets executed on logout or shutdown! Could you maybe expand your answer on what you mean with "location where it will be executed on logout or shutdown.", because I'm myself not aware of such a location! Commented Aug 16, 2023 at 13:06

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.