LFCS practice question 17 of 35

Which of the following commands will set the variable text to olaf is home? (Choose two)

Full set: 260 questions
Answer options
A: text=olaf\ is\ home Correct answer
B: text=$olaf is home
C: $text='olaf is home'
D: text=='olaf is home'
E: text="olaf is home" Correct answer
Correct Answer:
A. text=olaf\ is\ home; E. text="olaf is home"
Explanation

In Bash and other POSIX-compliant shells, variable assignment uses the variable=value syntax without spaces around the equals sign. When the value contains spaces, it must be quoted or the spaces must be escaped. Option A uses backslashes (\) to escape the spaces, treating them as literal characters. Option E uses double quotes (") to group the words into a single string argument. Both methods correctly assign the multi-word string "olaf is home" to the variable text.

Why Incorrect

B. text=$olaf is home: This attempts to assign the value of a variable named $olaf to text and then execute is home as a command, which is incorrect.
C. $text='olaf is home': The dollar sign ($) is used to retrieve a variable's value, not for assignment. This is a syntax error.
D. text=='olaf is home': The double equals (==) is a comparison operator used within conditional expressions, not the assignment operator (=).

References

GNU Bash Reference Manual. (2022). "3.4 Shell Parameters". In Bash Reference Manual. Free Software Foundation. Retrieved from https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Shell-Parameters

GNU Bash Reference Manual. (2022). "3.1.2.1 Quoting". In Bash Reference Manual. Free Software Foundation. Retrieved from https://www.gnu.org/savannah-checkouts/gnu/bash/manual/bash.html#Quoting

Sobell, M. G. (2018). A Practical Guide to Linux Commands, Editors, and Shell Programming. Pearson Education. Chapter 4, "The Bourne Again Shell," Section: "Variables: User-Created Variables".

Scroll to Top