Linux and Command-Line Basics
$ name=world
$ echo 'Hello $name'
$ echo "Hello $name"When the commands above are run in order, which of the following is the output of the two echo commands?
- AThe first is Hello world and the second Hello $name
- BBoth the first and the second are Hello world
- CThe first is Hello $name and the second Hello world
Show answer and explanation
Answer: C. The first is Hello $name and the second Hello world
In the shell, a part enclosed in single quotes ('...') is taken literally, and variables such as $name are not expanded. Inside double quotes ("...") variables are expanded, so the second becomes Hello world. Double quotes are also used to make a variable's value a single argument even if it contains spaces. Note that in the assignment "name=world", no spaces may be placed around the =.
- A. The roles of single and double quotes are reversed. Variables are expanded inside double quotes.
- B. Variables are not expanded inside single quotes.
- C. Correct. Single quotes keep the text as is, and double quotes expand variables.