#!/bin/sh

# Demonstrate reading passwords in a shell script
# while not echoing those passwords to the screen.

# Note `read -s` in bash 3 at least does not echo
# the return/newline either? Therefore we need to
# `echo >&2` after each read to simulate that.

getpassword() {
  until [ "${password}" = "${rpassword}" ] && [ -n "${password}" ]; do
    # shellcheck disable=SC2162,SC3045
    read -s -p "Enter a password for user '$1' : " password; echo >&2
    # shellcheck disable=SC2162,SC3045
    read -s -p "Reenter password for user '$1' : " rpassword; echo >&2
  done
  echo "${password}"
}

pw=$(getpassword "${1:-blah}")

echo "password is '${pw}'"
