Created
December 11, 2025 19:28
-
-
Save meineerde/22ee0c0f79e32e48b5754e94b25a632d to your computer and use it in GitHub Desktop.
A function to ask for a password for POSIX shell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/sh | |
| ask_password() { | |
| unset -v PASSWORD | |
| trap 'stty echo; return 1' INT EXIT; | |
| stty -echo | |
| while [ -z "$PASSWORD" ]; do | |
| printf "%s (CTRL+C to abort): " "$1" | |
| read -r PASSWORD | |
| printf "\n" | |
| if [ -n "$PASSWORD" ]; then | |
| stty echo; | |
| return 0 | |
| else | |
| echo "Empty password are not allowed. Please try again or press CTRL+C to abort." | |
| fi | |
| done | |
| } | |
| if ask_password "Multipass?"; then | |
| echo "You entered ${PASSWORD}" | |
| else | |
| echo "You aborted. No password was provided." | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment