Created
August 17, 2018 21:21
-
-
Save sgen/0e3394c973ea96f7a87216eed937280c to your computer and use it in GitHub Desktop.
Read a password discreetly in bash
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/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