Skip to content

Instantly share code, notes, and snippets.

@meineerde
Created December 11, 2025 19:28
Show Gist options
  • Select an option

  • Save meineerde/22ee0c0f79e32e48b5754e94b25a632d to your computer and use it in GitHub Desktop.

Select an option

Save meineerde/22ee0c0f79e32e48b5754e94b25a632d to your computer and use it in GitHub Desktop.
A function to ask for a password for POSIX shell
#!/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