Question

Difficulty: HardBasic Scripting Languages and Constructs

An IT analyst is reviewing a custom Bash shell script (`.sh`) designed to automatically rotate system logs on a Linux workstation when disk usage exceeds a specified percentage. The script contains the following line intended to set a threshold variable:

`THRESHOLD = 80`

When executing the script, the shell returns an error stating `THRESHOLD: command not found`. Which of the following correctly explains the cause of this failure and provides the proper syntax correction?

  1. Bash does not allow spaces around the assignment operator (`=`); removing the spaces to write `THRESHOLD=80` resolves the error.Answer
  2. B
    Bash requires all user-defined variables to be declared using the `set` keyword; changing the line to `set THRESHOLD=80` is required.
  3. C
    Bash variables storing numeric data must include a dollar sign prefix during assignment; changing the line to `$THRESHOLD=80` is required.
  4. D
    Bash scripts treat all unquoted numbers as command flags; enclosing the value in quotes as `THRESHOLD = "80"` is required.

Answer

In Bash shell scripts, variable assignment requires that no spaces precede or follow the equals sign (`=`). The line must be written as `THRESHOLD=80` without spaces.
In Linux Bash shell scripting (`.sh`), variable assignment is strict regarding whitespace. Writing `THRESHOLD = 80` causes the shell parser to look for a command named `THRESHOLD` and pass `=` and `80` to it as parameters. Removing the spaces around the equals sign (`THRESHOLD=80`) informs the parser that a variable is being assigned a value.

Step-by-Step Solution

1
Analyze the error message `THRESHOLD: command not found` emitted by the Bash interpreter.
Identified that the shell interpreted `THRESHOLD` as a command name rather than a variable assignment.
When spaces surround the `=` operator in Bash, the shell treats the text preceding the space (`THRESHOLD`) as an executable command and subsequent tokens as arguments.
2
Evaluate standard Bash variable assignment rules.
Determined that `NAME=value` with zero spaces around `=` is the required syntax for variable initialization in shell scripts.
Eliminating whitespace ensures the shell parser correctly recognizes the assignment construct.

Key Concept

Bash Shell Script Variable Syntax Rules
Estimated Time:1m 30s
Rate this question