Skip to content

Instantly share code, notes, and snippets.

@sgen
Created August 17, 2018 21:21
Show Gist options
  • Save sgen/0e3394c973ea96f7a87216eed937280c to your computer and use it in GitHub Desktop.
Save sgen/0e3394c973ea96f7a87216eed937280c to your computer and use it in GitHub Desktop.
Read a password discreetly in bash
#!/bin/bash
# Read secret string
read_secret() {
# Disable echo.
stty -echo
# Set up trap to ensure echo is enabled before exiting if the script
# is terminated while echo is disabled.
trap 'stty echo' EXIT
# Read secret.
read "$@"
# Enable echo.
stty echo
trap - EXIT
# Print a newline because the newline entered by the user after
# entering the passcode is not echoed. This ensures that the
# next line of output begins at a new line.
echo
}
read_secret -r -p 'Password: ' user_password;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment