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.
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)- A81% (38)
- B2% (1)
- C6% (3)
- D11% (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.
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.
$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.
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.
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.
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
Community Discussion
No community discussion yet for this question.