What is the purpose of .bash_logout?
From man bash
:
When a login shell exits, bash reads and executes commands from the files ~/.bash_logout and /etc/bash.bash_logout, if the files exists.
The .bash_logout
file does not have to exist.
Its contents is sourced by bash
when a bash
login shell exits. The file makes it possible to do, for example, various forms of cleanup when logging out from a terminal session.
It may be used to execute any shell code, but may be used to e.g. clear the screen if logins are done in a non-GUI environment. Some may also find it useful for explicitly terminating programs that were started from .bash_login
or .bash_profile
(if, for example, fetchmail
or some similar process is started as a user daemon or in the background, it may be good to terminate it in .bash_logout
).
The csh
shell has a similar file called .logout
and the corresponding file for the zsh
shell is called .zlogout
. The ksh
shell has to my knowledge no similar functionality.
See also the tangentally related question Difference between Login Shell and Non-Login Shell?
Since this question asks for details of .bash_logout file, it would be good to list various similar files. We usually have these 6 files for various purposes-
- /etc/profile
- ~/.bash_profile
- ~./bash_login
- ~/.bashrc
- ~/.profile
- ~/.bash_logout
In some cases you might also come across .bash_history file, which stores the commands which the user has executed.
Assuming that you are aware about .bashrc and .bash_login file, let's focus on the sequence of execution of these files and then we'll look at the purpose of .bash_logout file.
Firstly when the user logs in and if the .bash_profile file is available, it will be executed regardless of the existence of .bash_login or .profile file.
If that file is not available, then firstly the .bash_login file will be executed, and if this file is also not available, then .profile file will be executed.
Note that .bash_profile file is read and any commands in this file are executed when the user logs in, but this file is not read when the user starts a new shell. If the user starts a new shell, .bashrc file is read, for which you are quite aware of.
Comimg to the .bash_logout file, it is quite clear from the name that this file ks executed wjem the user logs out. As one might agree, that the primary purpose of bash is to provide an environment which makes work easier for the user. So this file helps in creating an environment which can help to execute some commands when the user logs out. There can be plethora of instances. For illustration, let's assume the admin wants to kill all the processes started by that user when the user logs out. Or, the user might want to clear the .mysql_history file. He/she might want to copy some files or take a backup while logging out. So you see, there can be many instances.
Let's assume the user wants to make a backup file of /etc directory everytime he logs out. So he might open the .bash_logout file and type this command -
tar -cvjf /etc ~/path/to/destination