Bash Read Username and Password without echoing back

Bash Read Username and Password

Bash Read Username and Password – In this Bash Tutorial, we shall learn to read username and password from user, with Password being not echoed back to shell.

Bash Read Username and Password without echoing

 

We shall use read command with following options,

read -p PROMPT <variable_name>  displays PROMPT without a new line and echoes input read from user, character by character. The input is read to bash variable. This option p  is suitable for reading Username.

read -sp PROMPT <variable_name>  displays PROMPT without a new line and reads input from user in silent mode (does not echo back user input). The input is read to variable. These options s,p  are suitable for reading Password.

 

Example Bash Shell Script to Read Username and Password

Note : An empty echo is provided after reading Password because read -sp  reads input in silent mode and does not echo back input including new line. However pressing enter key after password makes the input be loaded to password variable.
 

Example to Read Password with * echoed back to Shell

 

Recommended Reading : Bash Read User Input