5

Updated as original was unclear.

I'm setting up postfix on a computer to work with my email's SMTP.

I don't want to enter my credentials in plain text, especially when typing in public.

Is it possible to make bash not show my input for a command or two?

Example: I'm entering a series of commands and then I want to echo a username and password into a config file.

I'd like to quickly go into a mode where my input isn't shown on screen, then easily leave that mode with ctl+c.

2 Answers2

4

In bash: read is the command and the relevant option is (from man bash):

-s Silent mode. If input is coming from a terminal, characters are not echoed.

#!/bin/bash
unset password
prompt="Enter Password:"
while IFS= read -p "$prompt" -r -s -n 1 char
do
    if [[ $char == $'\0' ]]
    then
        break
    fi
    prompt='*'
    password+="$char"
done
echo
echo "Done. Password=$password"

But better to not echo anything as using asterisks allows others to see the lenght of the password.

  • 1
    Might more directly solve the OP's problem by reading the whole variable at once. – Jeff Schaller Oct 09 '17 at 14:22
  • Reading in one go allows interpretation of some characters. As an specific example, the key backspace removes one character from the string. Others are also possible. One character at a time allow the most precise capture of the string of characters typed. @JeffSchaller –  Oct 09 '17 at 14:56
3

You should use the stty command:

stty -echo

To re-enable echoing of terminal input:

stty echo
GAD3R
  • 66,769