Clear terminal and prevent restoration

terminal

There is some sensitive data (credit card numbers and such) that I store on my hard drive, encrypted. I use a script to decrypt the data and display it in the terminal window. When I am finished, I would like the script to clear the terminal, so that the sensitive data can no longer be viewed (even if someone gains access to my computer).

I have read here that +k clears the terminal output. To implement this in a script, I used AppleScript as suggested in the accepted answer to this question:

/usr/bin/osascript -e 'tell application "System Events" to tell process "Terminal" to keystroke "k" using command down'

Finally, my script closes the terminal window with

killall Terminal

The problem: if I leave the terminal window with the sensitive output open for about 30 seconds or longer before closing if, then the next time I open terminal, it restores the sensitive data (the text [restored] is displayed below it). If the data was initially displayed for less time, it is not restored.

How can I clear the terminal window from within a script, and prevent Terminal from restoring the cleared data?

I am running OS X 10.10.3.

Best Answer

Final Solution:

Remove the Terminal saved state, located in:

~/Library/Saved Application State/com.apple.Terminal.savedState/*

Then, make that folder read-only so that it can't be written to.


You can use the clear command to clear your terminal window from within a script. As noted on this other thread, the following commands in sequence will clear the current buffer, then the scroll back buffer:

clear && printf '\e[3J'

Please note:
(The clear command looks up the appropriate sequence for clearing the screen for the current terminal, but the “erase scroll-back” escape sequence is custom and must be hard-coded. If you put this in a shell script that you don’t know for certain will only ever be run with Terminal, you should check that $TERM_APPLICATION is Apple_Terminal before sending it.)


Alternative 1

Turn off Terminal.app's window restore functionality:

defaults write com.apple.Terminal NSQuitAlwaysKeepsWindows -bool false

Alternative 2

You can also store sensitive information in the Secure Notes section within the Keychain Access utility.

enter image description here

Related Question