010-160 · Question #87
Which of the following commands sets the variable USERNAME to the value bob?
The correct answer is E. USERNAME=bob. Shell variable assignment in bash uses the syntax NAME=value with no spaces around the equals sign and no keyword prefix.
Question
Options
- Aset USERNAME bob
- B$USERNAME==bob
- Cvar USERNAME=bob
- DUSERNAME==bob
- EUSERNAME=bob
How the community answered
(30 responses)- C3% (1)
- D3% (1)
- E93% (28)
Why each option
Shell variable assignment in bash uses the syntax NAME=value with no spaces around the equals sign and no keyword prefix.
The set command is used to set shell options or positional parameters, not to assign named variables - this syntax is invalid for variable assignment.
Prefixing a variable name with $ dereferences it for reading its value; using == is a comparison operator, making this an invalid assignment expression.
The var keyword is used in JavaScript and is not valid shell syntax for variable assignment in bash or POSIX shells.
The == operator performs comparison, not assignment - this expression does not set any variable value.
In bash and POSIX-compatible shells, a variable is assigned by writing the variable name immediately followed by = and the value with no spaces, as in USERNAME=bob. No dollar sign, keyword, or operator is used on the left side during assignment.
Concept tested: Bash shell variable assignment syntax
Source: https://www.gnu.org/software/bash/manual/bash.html#Shell-Parameters
Topics
Community Discussion
No community discussion yet for this question.