8

How can I write Bash Code for Hiding Password Or convert into '*' user input will be in string so no spaces, and change or hide password String.

Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
AlphaCoder
  • 163
  • 1
  • 3
  • 8
  • 1
    A string is a string even if it contains spaces, and passwords often contain spaces... – Kusalananda Dec 10 '18 at 20:33
  • Oooph, tough call for me on VTC here. The target Q does have *an* answer that would print asterisks, as asked ([in a comment](https://unix.stackexchange.com/questions/487164/required-bash-code-for-hide-password-during-typing#comment893666_487166)) in this question, but it doesn't handle [backspaces](https://unix.stackexchange.com/questions/487164/required-bash-code-for-hide-password-during-typing#comment893864_487166) as also hinted at here. I'll VTC because it's a good duplicate otherwise, but if this question is edited to incorporate new requirements, it *could* become separate. – Jeff Schaller Dec 11 '18 at 13:54

2 Answers2

13

Use read -s to not echo the input, i.e. show nothing when the user types the password:

read -p 'Password? ' -s password
echo Your password is "$password".
choroba
  • 45,735
  • 7
  • 84
  • 110
11

You can use the systemd-ask-password, the password will displayed as asterisks while typing.

Format: (systemd-ask-password --help)

systemd-ask-password [OPTIONS...] MESSAGE

e,g:

PASSWORD=$(systemd-ask-password "Please type your Password:")
Please type your Password: ***********
GAD3R
  • 63,407
  • 31
  • 131
  • 192