Bash_Scripts/Functions/Secure-Read-Input.bash
2021-07-28 09:32:08 -05:00

29 lines
630 B
Bash
Executable File

#!/bin/bash --posix
# shellcheck disable=SC2120
read_sensitive() {
# Securely read input into a variable
#
# Arguments:
# READVARIABLE <type: variable> <position: 1> <required: true>
# - A variable must be passed and the output of this function will be set to that variable
# e.g: read_sensitive MYVAR
#
# Usage:
# read_sensitive SOMEVAR
#
# POSIX Compliant:
# Yes
#
stty -echo
local INPUT
read -r INPUT
stty echo
printf "\n"
eval "${1}=${INPUT}"
}
printf "Please input a password: " && read_sensitive PASSWORD_VARIABLE
echo "Received your password as ${PASSWORD_VARIABLE}"