Desk Clarity

A field guide to everyday digital problems.

Developer tools / Practical guide

Bash Bracket Test Needs Separate Arguments: Check Spaces and Quoting

The [ form is a command whose final argument is ].

By ON Lab2 min readSources checked November 10, 2026 (KST)

The [ form is a command whose final argument is ]. Separate each operator and operand, and quote string expansions so empty or spaced input remains one argument.

Reproduce the behavior safely

Run this self-contained fixture in an empty temporary directory, separate from your project. Shell examples create only named fixture files there. JavaScript examples use local values and send no network requests.

value='two words'
if [ "$value" = 'two words' ]; then printf 'matched\n'; else exit 1; fi
empty=''
if [ -z "$empty" ]; then printf 'empty\n'; else exit 1; fi
# One nonempty argument is a true test, not a comparison:
if [ 'left=right' ]; then printf 'single nonempty argument is true\n'; fi
# A compact name such as [x is a different command token:
if command -v '[x' >/dev/null 2>&1; then exit 1; fi

Observed local output

matched
empty
single nonempty argument is true

Why the result changes

Spaces are how the shell forms the argument list; brackets are not punctuation that can be attached freely to operands. Even a command that runs can mean the wrong thing. The single argument left=right is nonempty, so it is true without comparing left and right. Printing or inspecting the argument structure is often more useful than changing the surrounding if statement.

Choose and verify the correction

Use [ "$left" = "$right" ] for a portable string equality test and keep the closing bracket separate. Use -eq only when the operands satisfy the intended integer policy. For several conditions, combine separate tests with shell && or || rather than building ambiguous long argument lists. When choosing Bash [[ syntax instead, document that dependency and learn its pattern behavior separately.

Limits and next checks

The fixture uses Bash builtins in a temporary shell. It does not claim every test implementation has identical diagnostics. A missing space can produce command-not-found, missing-bracket errors or a silently true one-argument test depending on the tokens. Quoting protects argument boundaries; it does not turn text into a numeric validator or disable pattern semantics in every other conditional interface.

Related: A Regex Variable Needs Different Quoting in Grep and Bash Match.

Isolated fixture executed on macOS on November 10, 2026 (KST), with GNU bash, version 3.2.57(1)-release (arm64-apple-darwin26). The observed output verifies this example, not every application or runtime.

Sources & further reading