LX0-103 · Question #26
What does the following command do? cat '$TEST'
The correct answer is B. Displays the contents of the file named $TEST if it exists.. Single quotes in bash prevent all shell expansion, so $TEST is treated as a literal string rather than a variable reference.
Question
What does the following command do? cat '$TEST'
Options
- ADisplays a bash syntax error message.
- BDisplays the contents of the file named $TEST if it exists.
- CWaits for the user to enter text and then echos the text back.
- DDisplays the contents of the file named inside the back quotes.
- EDisplays the contents of the file named by the environment variable TEST.
How the community answered
(60 responses)- A3% (2)
- B75% (45)
- C8% (5)
- D12% (7)
- E2% (1)
Why each option
Single quotes in bash prevent all shell expansion, so $TEST is treated as a literal string rather than a variable reference.
Single quotes are valid bash syntax; no syntax error is produced.
Single quotes suppress all forms of shell interpretation, including variable expansion. Because $TEST is enclosed in single quotes, bash does not expand it to the value of the environment variable TEST but instead treats '$TEST' as the literal filename $TEST. The command therefore attempts to display a file whose name is the four characters $TEST.
cat with a filename argument reads that file, not standard input; it will not wait for user input.
The command uses single quotes, not backticks; backticks would perform command substitution, but single quotes do not.
Variable expansion is suppressed by single quotes, so the variable TEST is never consulted; the literal string $TEST is used as the filename.
Concept tested: bash single-quote quoting and variable expansion suppression
Source: https://www.gnu.org/software/bash/manual/bash.html#Single-Quotes
Topics
Community Discussion
No community discussion yet for this question.