You have to change the terminal settings to do this. Using stty you can disable the canonical mode and disable the local echo (this is the same effect of read -s suggested by unSpawn). Then you can detect key strokes using the dd command and put the character corresponding to each key into the password. Echo a single asterisk (or a hash, if you prefer) and repeat until the user hits Enter. Finally restore the original settings and have the "recorded" password at your disposal.
Code:
#!/bin/bash
# ------------------------------------------------------------
# Prompt user for username and password
# ------------------------------------------------------------
read -p "please enter username and press [enter]: " username
echo -n "please enter password and press [enter]: "
# ------------------------------------------------------------
# Save current terminal settings
# ------------------------------------------------------------
oldstty=$(stty -g)
# ------------------------------------------------------------
# Disable canonical mode and local echo
# ------------------------------------------------------------
stty -icanon -echo
# ------------------------------------------------------------
# Get the first keystroke using dd
# ------------------------------------------------------------
key=$(dd bs=1 count=1 2>/dev/null)
# ------------------------------------------------------------
# While key is not "Enter" store key in the password,
# echo an asterisk and get another keystroke
# ------------------------------------------------------------
while [ x$key != x$(echo) ]
do
password=$password$key
echo -n \*
key=$(dd bs=1 count=1 2>/dev/null)
done
echo
# ------------------------------------------------------------
# Restore old terminal settings
# ------------------------------------------------------------
stty "$oldstty"
# ------------------------------------------------------------
# Check results
# ------------------------------------------------------------
echo $username
echo $password
How to capture keystrokes using dd is taken from example 15-58 of the Advanced Bash Scripting Guide.