LFCS · Question #858
Which of the following command sets the Bash variable named TEST with the content FOO?
The correct answer is D. TEST="FOO". In Bash, variable assignment follows a specific syntax where the variable name is immediately followed by an equals sign and then the value, without any spaces.
Question
Options
- Aset TEST="FOO"
- BTEST = "FOO"
- Cvar TEST="FOO"
- DTEST="FOO"
How the community answered
(25 responses)- A4% (1)
- B4% (1)
- D92% (23)
Why each option
In Bash, variable assignment follows a specific syntax where the variable name is immediately followed by an equals sign and then the value, without any spaces.
The `set` command is primarily used for manipulating shell options or positional parameters, not for direct variable assignment in this format.
Including spaces around the equals sign (`=`) in Bash variable assignments causes the shell to interpret the variable name as a command and the equals sign as an argument, resulting in a syntax error.
`var` is not a keyword used for variable declaration or assignment in Bash; variables are implicitly created upon their first assignment.
In Bash, the correct and standard syntax for assigning a value to a variable is `VARIABLE_NAME="VALUE"`, where there are no spaces immediately surrounding the equals sign, making `TEST="FOO"` the correct command.
Concept tested: Bash variable assignment syntax
Source: https://www.gnu.org/software/bash/manual/bash.html#Shell-Variables
Topics
Community Discussion
No community discussion yet for this question.