I have a script that automates a process that needs access to a password protected system. The system is accessed via a command-line program that accepts the user password as an argument.
I would like to prompt the user to type in their password, assign it to a shell variable, and then use that variable to construct the command line of the accessing program (which will of course produce stream output that I will process).
I am a reasonably competent shell programmer in Bourne/Bash, but I don't know how to accept the user input without having it echo to the terminal (or maybe having it echoed using '*' characters).
Can anyone help with this?
A POSIX compliant answer. Notice the use of
/bin/sh
instead of/bin/bash
. (It does work with bash, but it does not require bash.)I found to be the the
askpass
command usefulEvery input character is replaced by *. See: Give a password ****
This link is help in defining, * How to read password from use without echo-ing it back to terminal * How to replace each character with * -character.
https://www.tutorialkart.com/bash-shell-scripting/bash-read-username-and-password/
Here is another way to do it:
The
read -s
will turn off echo for you. Just replace theecho
on the last line with the command you want to run.Turn
echo
off usingstty
, then back on again after.One liner:
Under Linux (and cygwin) this form works in bash and sh. It may not be standard Unix sh, though.
For more info and options, in bash, type "help read".