The question "What is the purpose of .bashrc and how does it work?" sought the purpose and use of .bashrc. Another file with a similar name is .bash_logout.

Should this file exist in the first place? If so, what is the role of this file?


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?


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.


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-

  1. /etc/profile
  2. ~/.bash_profile
  3. ~./bash_login
  4. ~/.bashrc
  5. ~/.profile
  6. ~/.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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.