Skip to content

Instantly share code, notes, and snippets.

@dougpagani
Created December 15, 2020 23:49
Show Gist options
  • Save dougpagani/8e8b9fc761da1cf4bd91e131e9dfc9c4 to your computer and use it in GitHub Desktop.
Save dougpagani/8e8b9fc761da1cf4bd91e131e9dfc9c4 to your computer and use it in GitHub Desktop.
#!/bin/bash
unset PASSWORD
unset CHARCOUNT
echo -n "Enter password: "
stty -echo
CHARCOUNT=0
while IFS= read -p "$PROMPT" -r -s -n 1 CHAR
do
# Enter - accept password
if [[ $CHAR == $'\0' ]] ; then
break
fi
# Backspace
if [[ $CHAR == $'\177' ]] ; then
if [ $CHARCOUNT -gt 0 ] ; then
CHARCOUNT=$((CHARCOUNT-1))
PROMPT=$'\b \b'
PASSWORD="${PASSWORD%?}"
else
PROMPT=''
fi
else
CHARCOUNT=$((CHARCOUNT+1))
PROMPT='*'
PASSWORD+="$CHAR"
fi
done
stty echo
echo $PASSWORD
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment