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.)

  • 2
    Read the passwd from/dev/tty. – VPfB May 21 '17 at 6:33

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

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.