Interview Prep/Shell Scripting/Arrays, conditionals and loops

Why does `[ $var = "prod" ]` fail when `var` is empty or contains spaces?

IntermediateConceptArrays, conditionals and loops
Answer

[ is a command, so the unquoted variable is split before [ sees it. Empty var gives [ = prod ] → bash: [: =: unary operator expected. var="a b" gives [ a b = prod ] → bash: [: too many arguments.

[ "$var" = "prod" ]     # fixed
[[ $var == prod ]]      # Bash: no splitting inside [[ ]]
All Shell Scripting questions Roadmap: Shell scripting (Bash)

More on Arrays, conditionals and loops