Secrets and diagnostic logging
Secrets should not be placed in command arguments, source code, shell history or logs. Process arguments may be observable, and exported environment variables may be inherited or exposed depending on platform and privileges. Use a purpose-built credential mechanism, a restricted file or descriptor where supported by the receiving program, and minimize lifetime and access. Bash read -r -s suppresses terminal echo when reading from a terminal; it does not encrypt data and does not guarantee that later code will not leak it. Disable xtrace before reading or expanding credentials, and restore it only after clearing sensitive variables and completing secret-dependent work. Do not rely on regex redaction to remove every secret from arbitrary logs; prefer allowlisted structured fields that contain no credential values. The following examples use only synthetic tokens, never actual credentials. Pipe input in the lab is not a test of terminal echo behavior. Close the dedicated trace descriptor by unsetting BASH_XTRACEFD before inspecting its file. Verify that expected nonsensitive trace entries exist; absence of the fake token alone is not sufficient if tracing never worked.
Practical examples
Run each example as a separate script in a fresh noninteractive Bash 5.2 process and empty practice directory. Tests use C locale; do not enter expected output as code.
Read a synthetic token without printing it
umask 077printf 'DEMO_ONLY\n' > token.fixture || exit 1IFS= read -r -s token < token.fixture || exit 1[[ -n $token ]] || exit 2printf 'token present\n'unset tokenExpected stdout:
token presentThe input is a file in this fixture, not a terminal; no real credential is used.
Keep traces away from sensitive operations
umask 077exec 3> trace.log || exit 1BASH_XTRACEFD=3PS4='+ 'set -xprintf 'start\n' || exit 1set +xsynthetic=DEMO_ONLY[[ -n $synthetic ]] || exit 1unset syntheticset -xprintf 'done\n' || exit 1set +xunset BASH_XTRACEFD || exit 1grep -Fq "printf 'start" trace.log || exit 1grep -Fq "printf 'done" trace.log || exit 1grep -Fq DEMO_ONLY trace.logcase $? in 1) : ;; *) exit 1 ;; esacprintf 'trace checked\n' Expected stdout:
startdonetrace checkedThe check confirms the authored fake value does not appear in the trace. It is not a universal leak detector.
Log only approved fields
event=login_checkoutcome=acceptedprintf 'event=%s outcome=%s\n' "$event" "$outcome" >&2 || exit 1printf 'complete\n' Expected stdout:
completeExpected stderr:
event=login_check outcome=acceptedFixed labels and allowlisted values avoid recording credentials.
Common mistakes
Never print a secret to demonstrate successful reading. Turning off xtrace after expansion is too late.
Practice
Use a fake token and verify that only the status label appears on stdout, stderr and any trace file. Repeat with an empty token and expect rejection. Do not use real credentials.
Next: Resource limits and software provenance