Bash Error Guide: '[: missing ]' — Fix Unterminated Test Brackets
Fix Bash '[: missing "]"': close every test bracket, keep spaces around [ and ], and quote operands so an empty value can't swallow the closing bracket.
- #bash
- #automation
- #troubleshooting
- #errors
Stuck on this Bash & Python Automation error? Get the free incident triage checklist
A one-page PDF — the exact steps to isolate, fix, and verify a production error like this one. No spam, unsubscribe anytime.
Overview
Bash prints [: missing ']' when a [ test command starts but never receives its closing ] as a separate final argument:
./run.sh: line 5: [: missing `]'
[ is literally a command whose name is [, and it requires its last argument to be ]. That means ] must be surrounded by spaces so the shell passes it as its own word. Forgetting the bracket, jamming it against a value, or letting an unquoted operand consume it all produce this error.
Symptoms
- A conditional fails at parse-of-arguments time with
missing ']'. - The test looks like it has a
], but it’s glued to a variable ("$x"]) or missing a space. - It appears after refactoring a long
[ ... ]line or converting from[[ ]]. - Only some inputs trigger it — a hint that an empty operand is eating the bracket.
Common Root Causes
- No closing bracket at all —
[ -f "$f"with the]forgotten. - No space before
]—[ -f "$f"]passes the single word"$f"], so[never sees a bare]. - No space after
[—[-f "$f" ]runs a command literally named[-f. - Unquoted empty operand —
[ $x ]where$xis empty can leave the argument list malformed. - A
]that got quoted or escaped —[ -f "$f" "]" ]or\]isn’t recognized as the terminator. - Line continuation gone wrong — a
\newline that dropped the bracket.
Diagnostic Workflow
Show exactly what words [ receives, including whether ] is its own word:
f=/etc/hosts
set -- -f "$f"] # simulate the glued bracket
printf '[%s]\n' "$@" # each argument on its own line
# [-f]
# [/etc/hosts] <-- no standalone ] argument
Syntax-check and trace:
bash -n ./run.sh
bash -x ./run.sh 2>&1 | grep "missing\|'\['"
Prefer [[ ]], whose brackets are shell keywords and don’t need surrounding spaces the same way — a quick way to confirm the problem is [ ] argument tokenization.
Example Root Cause Analysis
A cleanup script checked for a lockfile:
if [ -f "$LOCKDIR/$SERVICE.lock"]; then
echo "already running"; exit 0
fi
It failed with:
cleanup.sh: line 2: [: missing `]'
There was a ] on the line — but no space before it. The shell tokenized "$LOCKDIR/$SERVICE.lock"] as a single argument, so [ received -f and /var/lock/api.lock] and never got a standalone ]. Adding the space fixes it:
if [ -f "$LOCKDIR/$SERVICE.lock" ]; then
echo "already running"; exit 0
fi
Better still, use [[ ]], which is more forgiving and supports richer tests:
if [[ -f "$LOCKDIR/$SERVICE.lock" ]]; then
echo "already running"; exit 0
fi
Prevention Best Practices
- Always put spaces around
[and]—[ ... ], never[... ]or[ ...]. The brackets are arguments, not punctuation. - Prefer
[[ ... ]]in Bash scripts — clearer, no word-splitting, and less bracket-spacing fragility. - Quote operands —
[ -f "$f" ]prevents an empty value from disturbing the argument list. - Syntax-check in CI with
bash -nand ShellCheck (SC1009/SC1073 flag the malformed test). - Format tests on one clear line or use proper
\continuation so the closing bracket is never dropped.
Quick Command Reference
[ -f "$f" ] # spaces around [ and ] are mandatory
[[ -f "$f" ]] # keyword form: no bracket-spacing traps
bash -n ./script.sh # catch the malformed test pre-run
shellcheck ./script.sh # flags missing/glued brackets
set -- -f "$f" ]; printf '[%s]\n' "$@" # inspect the argument words
Related Guides
- Bash Error Guide: ‘unary operator expected’ — another
[ ]failure from a missing operand. - [Bash Error Guide: ’: too many arguments’ — the opposite: word-splitting adds extra arguments.
- Bash Error Guide: ‘syntax error near unexpected token’ — related parse-time failures in conditionals.
Conclusion
[: missing ']' is [ telling you its closing bracket never arrived as its own word — usually a forgotten bracket or a missing space that glued ] onto a value. Keep spaces around [ and ], quote your operands, and prefer [[ ... ]] in Bash. A bash -n and ShellCheck pass in CI catches the malformed test before it ever runs.
Fixed it? Get 500 Bash & Python Automation & DevOps AI prompts — free
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.
Did this fix your issue?
Get 500 Battle-Tested DevOps AI Prompts — Free
500 battle-tested, copy-paste AI prompts engineered by a senior systems engineer — every one with fill-in placeholders and safety/back-out notes. Drop your email and it's yours.
- 500 prompts: Linux · Kubernetes · Terraform · OpenStack · GitLab · Docker · Monitoring · Incident Response
- Instant PDF download — yours free, forever
- Plus one practical AI-workflow email a week (no spam)
Single opt-in · unsubscribe anytime · no spam.