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?

  1. AThe first is Hello world and the second Hello $name
  2. BBoth the first and the second are Hello world
  3. 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.
Practice this topic Back to topic

Books on Linux and Command-Line Basics Ad

As an Amazon Associate I earn from qualifying purchases.

More Linux and Command-Line Basics questions

  1. As shown above, the script s.sh is run with arguments. Which of the following is the output?
  2. In a shell script, in which of the following cases is the condition of the if above true?
  3. Which of the following are the files in the directory after the session above?
  4. An executable script run.sh in the current directory gives "command not found" when run by typing "run.sh", but runs when typed as "./run.sh". Which of the following is the reason?
  5. When the commands above are run in order, which of the following is the pair of outputs of the two bash -c commands?