23

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?

0

3 Answers 3

21

The .bash_logout file does not have to exist.

Its content is sourced by bash when a bash login shell terminates. The file makes it possible to do, for example, various forms of cleanup when logging out from a terminal session.

You can use it to run any shell code, such as clearing the screen in non-GUI environments during logouts. Additionally, some users may find it handy for explicitly terminating processes that were initiated from .bash_login or .bash_profile. For example, if you've started a user daemon or background process like fetchmail, you can terminate it conveniently within .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?

1
  • ~/.bash_logout is often used to release authentication, so it might contain /usr/bin/sudo -K or [ -f $HOME/.vault-token ] && /usr/bin/rm -f $HOME/.vault-token. Also a good place to decide not to keep the command history, such as with history -c. It's a good idea to have /usr/bin/clear as the last line, so there's no record of your session left on a terminal.
    – vk5tu
    Commented Aug 26, 2021 at 6:19
15

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.

0
8

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.

Coming to the .bash_logout file, it is quite clear from the name that this file is executed when 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. They 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 they log out. So they might open the .bash_logout file and type this command:

tar -cvjf /etc ~/path/to/destination
0

You must log in to answer this question.

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