Page 1 of 1

Shell Scripting Tip: Password Prompts

Posted: Tue Aug 06, 2013 11:10 am
by scott
Just something I was kicking around here, it was clever enough that I wanted it saved somewhere. The following bash script would present you a prompt for a password, and echo * for the output:

Code: Select all

                                        echo -n "Enter password: "
                                        # Suppress output of password.
                                        unset PASSWORD 

                                        while IFS= read -r -s -n1 pass < /dev/tty  ; do
                                                if [[ -z $pass ]]; then
                                                        echo
                                                        break
                                                else
                                                        echo -n '*'
                                                        PASSWORD+=$pass
                                                fi
                                        done
This is generating the * using IFS (internal field separator) trickery. The limitation of this is that all characters, including backspace, ctrl-h, ctrl-u would be interpreted as *.