nerdexam
LPI

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.

The Power of the Command Line

Question

Which of the following commands sets the variable USERNAME to the value bob?

Options

  • Aset USERNAME bob
  • B$USERNAME==bob
  • Cvar USERNAME=bob
  • DUSERNAME==bob
  • EUSERNAME=bob

How the community answered

(30 responses)
  • C
    3% (1)
  • D
    3% (1)
  • E
    93% (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.

Aset USERNAME bob

The set command is used to set shell options or positional parameters, not to assign named variables - this syntax is invalid for variable assignment.

B$USERNAME==bob

Prefixing a variable name with $ dereferences it for reading its value; using == is a comparison operator, making this an invalid assignment expression.

Cvar USERNAME=bob

The var keyword is used in JavaScript and is not valid shell syntax for variable assignment in bash or POSIX shells.

DUSERNAME==bob

The == operator performs comparison, not assignment - this expression does not set any variable value.

EUSERNAME=bobCorrect

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

#shell variables#variable assignment#bash syntax

Community Discussion

No community discussion yet for this question.

Full 010-160 Practice