Member-only story
How Do You Show Asterisks for a Bash Password Prompt?
Someone asked how to use a Bash script to prompt for a password while displaying an asterisk for every character entered. Bash can already hide input with read -s, but that leaves the terminal blank while the user types. The interesting part of the question is making the script acknowledge each character with * while keeping the actual password hidden.
There are several ways to approach it. Two versions that came to my mind are:
The first approach
The basic idea is to read one character at a time, suppress its normal terminal output, print an asterisk, and append the character to a variable:
a=''
while read -n 1 -s c; do
[ "$c" = '' ] && break
printf '*'
a="$a$c"
doneread -n 1 makes Bash read a single character at a time, while -s prevents the character from being echoed to the terminal. The script then prints its own * and stores the real character in a.
Press Enter and the loop ends, leaving the complete password in the variable.
There is a catch, though. This version has no concept of editing. If you mistype a character and press Backspace, the script does not remove the previous character from a, and the asterisk remains on the screen.
For a quick demonstration, that may be enough. A password prompt that behaves like an ordinary input field should do better.




