Clearing the Screen When You Log Out

Problem

You use or administer some systems that do not clear the screen when you log out, and you’d rather not leave the tail end of whatever you were working on visible, since that could be an information leak.

Solution

Put the clear command in your ~/.bash_logout:.

# ~/.bash_logout

# Clear the screen on exit from the shell to prevent information leaks,
# if not already set as an exit trap in bash_profile
[ "$PS1" ] && clear

Or set a trap to run clear on shell termination:

# ~/.bash_profile
# Trap to clear the screen on exit from the shell to prevent
# information leaks, if not already set in ~/.bash_logout
trap ' [ "$PS1" ] && clear ' 0

Note that if you are connecting remotely and your client has a scroll-back buffer, whatever you were working on may still be in there. clear also has no effect on your shell’s command history.

Discussion

Setting a trap to clear the screen is probably overkill, but could conceivably cover an error situation in which ~/.bash_logout is not executed. If you are really paranoid you can set both, but in that case you may also wish to look into TEMPEST and Faraday cages.

If you skip the test to determine whether the shell is interactive, you’ll get errors like this under some circumstances:

# e.g., from tput
No value for $TERM and no -T specified

# e.g., from clear
TERM environment variable not set.

Get bash Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.