Interview Prep/Shell Scripting/Variables, quoting and expansions

How do you safely read a secret or password in a script?

IntermediateConceptVariables, quoting and expansions
Answer
read -rsp 'DB password: ' DB_PASS; echo

-s disables echo, -p prints a prompt. In automation there is no terminal, so read from a file with tight permissions or a secrets manager:

DB_PASS=$(< /run/secrets/db_password)                         # Docker/K8s secret file
DB_PASS=$(aws secretsmanager get-secret-value --secret-id prod/db \
  --query SecretString --output text)

Never pass secrets as command-line arguments (visible in ps and /proc/PID/cmdline), never echo them, and turn off set -x around them.

All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Variables, quoting and expansions