nerdexam
LPI

010-100 · Question #66

Which of the following commands will set the variable text to olaf is home? (Select TWO answers)

The correct answer is A. text=olaf\ is\ home E. text="olaf is home". Shell variable assignment requires no $ on the left-hand side, no spaces around =, and spaces within the value must be quoted or escaped.

The Power of the Command Line

Question

Which of the following commands will set the variable text to olaf is home? (Select TWO answers)

Options

  • Atext=olaf\ is\ home
  • Btext=$olaf is home
  • C$text='olaf is home'
  • Dtext=='olaf is home'
  • Etext="olaf is home"

How the community answered

(47 responses)
  • A
    81% (38)
  • B
    2% (1)
  • C
    6% (3)
  • D
    11% (5)

Why each option

Shell variable assignment requires no $ on the left-hand side, no spaces around =, and spaces within the value must be quoted or escaped.

Atext=olaf\ is\ homeCorrect

Backslash-escaping each space (olaf\ is\ home) is valid POSIX shell syntax that treats the escaped spaces as literal characters within the value, correctly assigning the full string to text.

Btext=$olaf is home

$olaf would be expanded as a separate variable (likely empty), and 'is home' would be parsed as a command to execute rather than part of the assignment, causing an error.

C$text='olaf is home'

Variable names on the left side of an assignment must not be prefixed with $; the $ sigil is only used when reading a variable's value, making this syntax invalid.

Dtext=='olaf is home'

The double equals operator == is a comparison operator, not an assignment operator; this would assign the literal string ='olaf is home' (with the leading =) to text, not the intended value.

Etext="olaf is home"Correct

Enclosing the value in double quotes ("olaf is home") prevents word splitting, so the entire string including spaces is assigned to text as a single value.

Concept tested: Shell variable assignment syntax with spaces

Source: https://www.gnu.org/software/bash/manual/html_node/Shell-Parameters.html

Topics

#shell variables#variable assignment#quoting#bash syntax

Community Discussion

No community discussion yet for this question.

Full 010-100 Practice