Skip to content
Latchkey

bash "unary operator expected" (unquoted empty var) in CI

Inside [ ... ], an unquoted variable that expands to nothing disappears entirely, so [ $VAR = yes ] becomes [ = yes ]. test then sees = with no left operand and reports "unary operator expected".

What this error means

A conditional works when the variable is set but fails intermittently with "[: =: unary operator expected" (or "unary operator expected") when the variable is empty or unset in CI.

bash
check.sh: line 8: [: =: unary operator expected

Common causes

An unquoted variable expands to empty

When $VAR is empty, [ $VAR = yes ] loses the left operand and test sees an ill-formed expression, so it errors instead of returning false.

A CI secret or env var is not populated

A variable set in one context but empty in the CI job (an unset secret, a skipped export) triggers the collapse only in the pipeline.

How to fix it

Quote the variable

Wrapping the expansion in double quotes preserves an empty argument, so the test compares "" against the literal instead of vanishing.

bash
if [ "$VAR" = "yes" ]; then
  echo enabled
fi

Use [[ ]] in bash

Bash's [[ ]] does not word-split, so an empty variable does not break the expression. Keep [ ] for POSIX sh scripts and quote there.

bash
if [[ $VAR == "yes" ]]; then
  echo enabled
fi

How to prevent it

  • Always quote variables inside [ ].
  • Prefer [[ ]] in bash-only scripts.
  • Run shellcheck, which flags unquoted test operands.

Related guides

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →