Keywords: Bash scripting | variable assignment | syntax error | Shell parsing | command line arguments
Abstract: This article provides an in-depth analysis of the common "command not found" error in Bash script variable assignments. By examining Shell syntax specifications, it details how spaces around the equals sign affect semantic interpretation, including command execution, argument passing, and environment variable settings. The article offers correct variable assignment syntax examples and explores Bash's mechanism for parsing simple commands, helping developers fundamentally understand and avoid such errors.
Problem Phenomenon and Error Analysis
In Bash script development, beginners frequently encounter a typical error scenario: when attempting to execute scripts containing variable assignments, the system returns a "command not found" error message. Specifically demonstrated by the following code example:
#!/bin/bash
STR = "Hello World"
echo $STR
When executing this script, the system reports: test.sh: line 2: STR: command not found. This error appears simple but actually involves core principles of Shell language parsing mechanisms.
Syntax Parsing Mechanism
According to POSIX Shell specifications, Bash follows specific syntax rules when parsing simple commands. A simple command consists of optional variable assignments, redirection operations, and command words, which can appear in any order.
The key point is: when the parser encounters strings containing equals signs, it determines their semantics based on the position and context of the equals sign:
- If the string starts with a valid variable name, followed by an equals sign, and non-empty content after the equals sign, it is treated as a variable assignment
- If the string doesn't meet the above conditions, it is treated as a regular command word or argument
Detailed Analysis of Space Impact
Scenario 1: Spaces on Both Sides of Equals Sign
Consider the following code:
STR = "foo"
The Bash parser interprets this line as: attempt to execute a command named STR, passing two arguments: the string = and the string foo. Since no executable command named STR exists in the system, it produces the "command not found" error.
From a semantic equivalence perspective, this is exactly equivalent to:
STR "=" "foo"
Scenario 2: No Space Before Equals, Space After Equals
Another common erroneous写法:
STR =foo
In this case, Bash still recognizes STR as a command name, while treating =foo as a single argument passed to that command. Equivalent to:
STR "=foo"
Scenario 3: Space Before Equals, No Space After Equals
The third error variant:
STR= foo
The semantics of this写法 are completely different: Bash recognizes STR= as a variable assignment (setting STR to an empty string), then attempts to execute a command named foo. Equivalent to:
STR="" foo
Correct Variable Assignment Syntax
According to Shell language specifications, correct variable assignment syntax requires no spaces on either side of the equals sign:
STR="Hello World"
Or omit quotes (when the value contains no spaces or special characters):
STR=Hello
Underlying Parsing Principles
When parsing command lines, Bash's lexical analyzer processes tokens in the following order:
- Identify redirection operators (such as
>,<) - Identify variable assignments (format:
name=value) - Identify the remaining parts as command names and arguments
When encountering STR = "foo", because there's a space before the equals sign, the parser doesn't recognize it as a variable assignment, but instead treats STR as a command name, with = and foo as arguments.
Practical Development Recommendations
To avoid such errors, developers should:
- Always ensure no spaces on either side of the equals sign in variable assignments
- Use quotes to enclose string values containing spaces
- Explicitly declare variable types in complex scripts
- Utilize Shell checking tools (like shellcheck) for syntax validation
Understanding Bash's parsing mechanism not only helps avoid basic syntax errors but also enables developers to write more robust and maintainable Shell scripts. This deep understanding of language underlying working principles is a key factor distinguishing junior from senior Shell programmers.