Why equal to operator does not work if it is not surrounded by space?

test (or [ expr ]) is a builtin function. Like all functions in bash, you pass its arguments as whitespace separated words.

As the man page for bash builtins states: “Each operator and operand must be a separate argument.”

It’s just the way bash and most other Unix shells work.

Variable assignment is different.

In bash a variable assignment has the syntax: name=[value]. You cannot put unquoted spaces around the = because bash would not interpret this as the assignment you intend. bash treats most lists of words as a command with parameters.

E.g.

# call the command or function 'abc' with '=def' as argument
abc =def

# call 'def' with the variable 'abc' set to the empty string
abc= def

# call 'ghi' with 'abc' set to 'def'
abc=def ghi

# set 'abc' to 'def ghi'
abc="def ghi"

Leave a Comment