Read password from keyboard instead of stdin

inputterminalzsh

I have a zsh script that prompts for keyboard entry:

read -rs 'pw?Password: '

This script also needs to read the contents of a file from stdin. But the read command takes the first line of stdin and uses it as input for the password:

myscript <<< 'line1\nline2'    # pw == 'line1'

Any way to get around this? I'd prefer a zsh or bash solution, though I'm open to others too.

(FWIW, gpg2 does this, so I know it's possible… just don't know how.)

Best Answer

The terminal is always accessible through /dev/tty, regardless of where standard input has been redirected from.

read -rs 'pw?Password: ' </dev/tty
Related Question