#!/bin/bash # # Read and echo a password, echoing responsive 'stars' for input characters # Also handles: backspaces, deleted and ^U (kill-line) control-chars # # This is the minimal shell script to do 'star' password reading # but it is not the best one. For the more advanced one see... # https://antofthy.gitlab.io/software/#askpass_stars # #### # # Anthony Thyssen, 22 May 2014 # unset PWORD PWORD= echo -n 'password: ' 1>&2 while IFS= read -r -n1 -s char; do # Convert users key press to hexadecimal character code # Note a 'return' or EOL, will return a empty string # #code=$( echo -n "$char" | od -An -tx1 | tr -d ' \011' ) code=${char:+$(printf '%02x' "'$char'")} # set to nothing for EOL case "$code" in ''|0a|0d) break ;; # EOL, newline, return 08|7f) # backspace or delete if [ -n "$PWORD" ]; then PWORD="$( echo "$PWORD" | sed 's/.$//' )" echo -n $'\b \b' 1>&2 fi ;; 15) # ^U or kill line echo -n "$PWORD" | sed 's/./\cH \cH/g' >&2 PWORD='' ;; [01]?) ;; # Ignore ALL other control characters *) PWORD="$PWORD$char" echo -n '*' 1>&2 ;; esac done echo echo $PWORD