Five lines whose only job is to refuse a deploy when the token is missing. I ran it with the token unset, then with the token set to an empty string:
$ cat deploy-guard.sh
#!/bin/bash
if [ -n $DEPLOY_TOKEN ]; then
echo "token present, deploying"
else
echo "no token, refusing" >&2; exit 1
fi
$ env -u DEPLOY_TOKEN bash deploy-guard.sh
token present, deploying
exit=0
$ DEPLOY_TOKEN="" bash deploy-guard.sh
token present, deploying
exit=0
No token, and the guard waved it through. Twice. Exit 0 both times, nothing on stderr, nothing for a CI log to flag. The "else" branch, the one I wrote the script for, cannot run at all.
I wrote this on purpose in a scratch directory, and it still took me a second look to see why. Every other test with an empty variable at least complains. This one agrees with you.
The empty variable is not there to test
[ is a command, not syntax, and its arguments go through word splitting like any other command's. An unquoted empty variable does not become one empty argument. It becomes zero arguments. So [ -n $DEPLOY_TOKEN ] reaches test as [ -n ].
With one argument, test has a rule: true if that argument is a non-empty string. The one argument is the literal -n, which is two characters long. True.
$ [ -n ] && echo "true: -n is a non-empty string"
true: -n is a non-empty string
exit=0
That is the same mechanism behind the error most people search for. Put a binary operator after the empty variable and the arithmetic version breaks loudly instead:
$ [ $count -eq 1 ] && echo one
bash: line 1: [: -eq: unary operator expected
exit=2
[ $count -eq 1 ] arrives as [ -eq 1 ]. Two arguments, so test tries to read -eq as a one-argument operator like -f or -z, cannot, and exits 2. Inside an if, 2 counts as false, so the script takes the other branch and keeps going. Fill the variable with a space instead and the same line fails from the other side:
$ name="john smith"; [ $name = admin ]
bash: line 1: [: too many arguments
exit=2
Too few words, too many words, or, with -n, exactly the wrong number of words and a confident answer. One bug, three outcomes, and the quietest one is the one guarding a deploy.
Fix one: quote it
$ grep if guard-fixed.sh
if [ -n "$DEPLOY_TOKEN" ]; then
$ env -u DEPLOY_TOKEN bash guard-fixed.sh
no token, refusing
exit=1
Quoted, an empty value stays one empty argument. [ -n "" ] is a two-argument test, -n is read as the operator, and an empty string fails it. For the -eq case, quoting turns unary operator expected into integer expected, which is the honest version of the error: the value is empty and a number was needed.
[[ ]] gets there too, because it does no word splitting at all. It is the right default in a bash script. It is not a full fix, though: [[ $count -eq 0 ]] is true for an empty count, because inside [[ ]] arithmetic an empty string evaluates to 0. The error goes away and the wrong answer stays.
Fix two: make empty mean something on purpose
The guard should never have been testing whether the token looked non-empty. It should have stopped the script if the token was missing. Two ways, both run here with the token unset:
$ env -u DEPLOY_TOKEN bash -u guard-fixed.sh
guard-fixed.sh: line 2: DEPLOY_TOKEN: unbound variable
exit=1
$ env -u DEPLOY_TOKEN bash -c ': "${DEPLOY_TOKEN:?DEPLOY_TOKEN must be set}"'
bash: line 1: DEPLOY_TOKEN: DEPLOY_TOKEN must be set
exit=127
set -u (the u in set -euo pipefail) catches the unset case and names the variable, which no form of the [ error ever does. It does not catch DEPLOY_TOKEN="", because an empty variable is still set. The :? expansion catches both, and its message is whatever you write. Put that line at the top of the script, once per required variable, and the test further down cannot be reached with a bad value.
Fix three: let ShellCheck read it first
$ shellcheck deploy-guard.sh
In deploy-guard.sh line 2:
if [ -n $DEPLOY_TOKEN ]; then
^-----------^ SC2070 (error): -n doesn't work with unquoted arguments. Quote or use [[ ]].
^-----------^ SC2086 (info): Double quote to prevent globbing and word splitting.
SC2070 is rated an error, a step above most of what ShellCheck reports, because [ -n $var ] is not loudly wrong, it is silently wrong. SC2086 is "info", the lowest severity, and the one people filter out first. It is the warning for this entire family of bugs. Run shellcheck in a pre-commit hook or a CI step and neither line reaches a server.
Why it bites in cron and CI, not on your laptop
The guard works every time the token is set, which is every time you test it by hand. It fails the first time the value is missing: a variable cron does not export, a secret that did not get injected into a CI job, an empty file read with $(cat token.txt). That is why the unary operator expected version shows up in a 2am log and never in testing, and why the -n version does not show up at all until someone asks how a deploy ran without credentials.
The full breakdown, with the same expression under [, quotes, [[, a default and set -u side by side: https://bashsnippets.xyz/snippets/unary-operator-expected
If ShellCheck is already flagging your scripts, the SC2086 deep dive covers the quoting warning behind this error, and Bash If Else Examples lists every test operator and which ones need quotes. The rest of the library is at https://bashsnippets.xyz
I bundled the scripts I copy onto every new server, plus 10 bonus ones, into the Production Bash Toolkit. 30-day refund if it isn't useful.
Top comments (0)