Shell Scripting Tip: Password Prompts

General Discussion of atomic repo and development projects.

Ask for help here with anything else not covered by other forums.
scott
Atomicorp Staff - Site Admin
Atomicorp Staff - Site Admin
Posts: 8355
Joined: Wed Dec 31, 1969 8:00 pm
Location: earth
Contact:

Shell Scripting Tip: Password Prompts

Unread post 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 *.
Post Reply