Shell – How to output “temporarily” to the shell, like zsh tab completion does

promptshellterminalzsh

In zsh shell, when you hit Tab twice to get tab completion, your options are displayed below your prompt. But when you make a selection, the options disappear.

I have a little script that outputs passwords to stdout based on keyword search. The only problem is, after I've copied and pasted and I'm done, the passwords stay in the terminal. I would really like to implement this "temporary" output for my passwords script. Is this possible?

Best Answer

On any system that uses terminfo:

printf "The password is %s " "swordfish"
read -r line
tput cuu1; tput el

Press Enter to erase the password and quit, or Ctrl+C to quit immediately. If your system uses termcap, use tput up; tput ce. This code assumes that the cursor is still on the line with the password by the time the user pressed Enter.