
In many regulated environments, privileged sessions are recorded. When you SSH into a production server, a session recording tool captures what appears on your screen, and sometimes what you type, so the session can be reviewed later by auditors or the security team.
That’s a good control, but it creates a new problem. The quick check every administrator does after loading a secret, echo $API_TOKEN, now writes that secret straight into a recording that may be stored for years and watched by people who should never see it.
This article builds on our earlier guide, How to Securely Handle Passwords on the Linux Command Line. It shows easy ways to confirm a secret is loaded correctly without ever displaying it in full.
What a session recording can capture
Recording tools differ, but they generally capture one or both of:
- Output: everything printed to your terminal. If a command prints a secret, it’s in the recording.
- Input: the keys you type. Text typed while the terminal’s echo is turned off, as it is with
read -s, is never printed, so output recordings don’t contain it. Keystroke auditing such as Linux’spam_tty_auditalso skips echo-off input by default, unless it’s configured with itslog_passwdoption.
Check how your own recording tool is configured, but the safe rule is simple: never make the terminal print the secret.
Step 1: Read the secret without echoing it
Load the secret with read, so it’s never typed as part of a command and never shown on screen. In Bash:
read -rs -p "API token: " API_TOKEN && echo
export API_TOKEN
In Zsh, the prompt goes after the variable name:
read -rs 'API_TOKEN?API token: ' && echo
export API_TOKEN
-sturns off echo, so nothing you type or paste appears.-rkeeps backslashes exactly as you typed them. Our earlier article suggested doubling backslashes; with-r, you don’t need to.- The trailing
&& echojust moves the cursor to a new line after you press Enter.
Step 2: Check the secret without printing it
Each of these confirms something useful about the secret while revealing little or nothing. They work in both Bash and Zsh.
Is it set?
[ -n "$API_TOKEN" ] && echo "API_TOKEN is set" || echo "API_TOKEN is empty or unset"
Is it the length you expect?
A stray space or a partial paste usually shows up as the wrong length:
echo "API_TOKEN is ${#API_TOKEN} characters"
Does it start and end the way it should?
Showing just a few characters at each end is usually enough to confirm you pasted the right value:
printf 'API_TOKEN: %s...%s\n' "${API_TOKEN:0:4}" "${API_TOKEN: -4}"
API_TOKEN: ghp_...76Wq
The space in ${API_TOKEN: -4} matters: it tells the shell to count from the end.
Show it masked, except the last four characters
This is the familiar “card number” style, with every character but the last four replaced by *:
printf '%s%s\n' "$(printf '%*s' $(( ${#API_TOKEN} - 4 )) '' | tr ' ' '*')" "${API_TOKEN: -4}"
***************76Wq
Compare a fingerprint
For long, random secrets such as API tokens and keys, you can compare a short fingerprint of the value with one recorded in your password vault, without either side showing the secret. On Linux:
printf '%s' "$API_TOKEN" | sha256sum | cut -c1-12
On macOS, use shasum -a 256 instead of sha256sum. If the 12 characters match the fingerprint you expect, the secret is right.
Only fingerprint long, random secrets. A hash of a short or guessable password can be cracked offline by anyone who sees it in the recording. For human-chosen passwords, stick to the length and ends checks above.
Does it match another value, or an expected format?
Compare two secrets, for example the one you loaded and the one in a config file, without printing either:
[ "$API_TOKEN" = "$EXPECTED_TOKEN" ] && echo "match" || echo "different"
Or check that it has the shape you expect:
case "$API_TOKEN" in
ghp_*) echo "Looks like a GitHub token" ;;
*) echo "Unexpected format" ;;
esac
A reusable peek function
Add this to your ~/.bashrc or ~/.zshrc to check any secret in one short command. It shows the length and only three characters at each end, and nothing at all for short values, where even a few characters would give too much away:
peek() {
local v="$1"
if [ -z "$v" ]; then echo "empty or unset"; return 1; fi
if [ "${#v}" -le 12 ]; then printf '%d chars (too short to show any of it)\n' "${#v}"; return 0; fi
printf '%d chars, starts %s..., ends ...%s\n' "${#v}" "${v:0:3}" "${v: -3}"
}
peek "$API_TOKEN"
19 chars, starts ghp..., ends ...6Wq
Calling a shell function doesn’t start a new process, so the value doesn’t appear in the system’s process list.
On Windows: PowerShell
PowerShell has its own equivalents. Read the secret without echoing it:
$sec = Read-Host -AsSecureString -Prompt 'API token'
$env:API_TOKEN = [System.Net.NetworkCredential]::new('', $sec).Password
Then check it without displaying it:
"Length: $($env:API_TOKEN.Length)"
"Ends in: ...$($env:API_TOKEN.Substring($env:API_TOKEN.Length - 4))"
if ($env:API_TOKEN) { 'API_TOKEN is set' }
And, in PowerShell 7, compare a fingerprint that matches the Linux and macOS commands above:
$bytes = [System.Text.Encoding]::UTF8.GetBytes($env:API_TOKEN)
[System.Convert]::ToHexString([System.Security.Cryptography.SHA256]::HashData($bytes)).ToLower().Substring(0, 12)
Common ways secrets end up in recordings
Even careful administrators trip over these:
echo,printenv,env,export -p,declare -pandset. All of them print variable values, and the last few print every variable at once.set -x(trace mode). Bash and Zsh print each command with its variables already expanded, so a simple[ -n "$API_TOKEN" ]shows the full secret. Turn tracing off before handling secrets.- Secrets on the command line. A value passed as an argument, like
--password "$API_TOKEN", is visible to other users in the process list while the command runs. Prefer tools that read secrets from an environment variable or standard input, such asdocker login --password-stdin. - Viewing files that hold secrets.
cat .envor opening a config file in an editor prints every value in it. - Error messages. Some tools echo the bad value back when authentication fails. Check what a tool prints before you trust it with a recorded session.
Step 3: Clean up
When you’re done, remove the secret from your session:
unset API_TOKEN
In PowerShell:
Remove-Item Env:API_TOKEN
Quick reference
# Load it without echo (Bash; in Zsh: read -rs 'API_TOKEN?API token: ')
read -rs -p "API token: " API_TOKEN && echo
export API_TOKEN
# Check it without showing it
[ -n "$API_TOKEN" ] && echo "set"
echo "${#API_TOKEN} characters"
printf '%s...%s\n' "${API_TOKEN:0:4}" "${API_TOKEN: -4}"
printf '%s' "$API_TOKEN" | sha256sum | cut -c1-12 # macOS: shasum -a 256
# Clean up
unset API_TOKEN
Need help designing privileged access controls or session recording that satisfies your auditors without exposing secrets? Talk to our team.
Header image: Photo by Jake Walker on Unsplash
