Run user-specific cleanup when a Bash login shell exits normally.
Which file should contain the cleanup?
~/.bash_logout is read when a login Bash exits. It is distinct from the profile and bashrc files used at startup.
Detailed explanation
~/.bashrcIncorrect. ~/.bashrc is an interactive non-login startup file.
Incorrect. ~/.bashrc is an interactive non-login startup file.
~/.profileIncorrect. ~/.profile is read during login initialization.
Incorrect. ~/.profile is read during login initialization.
/etc/profileIncorrect. /etc/profile is a system-wide login startup file.
Incorrect. /etc/profile is a system-wide login startup file.
~/.bash_logoutCorrect. ~/.bash_logout is the user-specific login-shell exit file.
Correct. ~/.bash_logout is the user-specific login-shell exit file.
Try it yourself
An example you can run in a temporary verification environment.
man bash 2>/dev/null | col -b | grep -A4 'When a login shell exits' | sed -n '1,5p'Expected result
~/.bash_logoutを読む旨。manがない最小環境では出力なしKey points
- User logout file
- Applies to login Bash
- Different timing from startup files
Notes
- Environment: GNU Bash 5.2 / マニュアル参照
- Command output formatting can vary slightly by distribution or tool version.
- Run the example in a temporary directory or process when possible.
Foundation review
Read the scope first
Check whether the command acts on the current shell, a new process, an existing process, or a file.
Verify the observable result
Use the supplied command and compare the output with the expected result.