LFCS · Question #839
What does the following command do? cat '$TEST'
The correct answer is B. Displays the contents of the file named $TEST if it exists. The single quotes around $TEST prevent the shell from expanding the TEST environment variable, causing cat to attempt to display the contents of a file literally named "$TEST".
Question
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
(38 responses)- B92% (35)
- C5% (2)
- E3% (1)
Why each option
The single quotes around `$TEST` prevent the shell from expanding the `TEST` environment variable, causing `cat` to attempt to display the contents of a file literally named "$TEST".
This command is syntactically valid in Bash; it merely attempts to open a file with a literal name.
Single quotes (`' '`) prevent all shell expansions, including variable expansion. Therefore, `cat '$TEST'` will interpret `$TEST` as a literal filename string and attempt to display the contents of a file literally named "$TEST" in the current directory if it exists.
`cat` without any arguments would wait for user input, but providing a filename, even a literal one, directs `cat` to that file.
Backticks (`` ` ``) are used for command substitution, not single quotes; single quotes explicitly prevent such substitutions.
The single quotes explicitly prevent the expansion of the `TEST` environment variable; if expansion were desired, the quotes would be omitted (e.g., `cat $TEST`).
Concept tested: Shell quoting (single quotes)
Source: https://www.gnu.org/software/bash/manual/bash.html#Single-Quotes
Topics
Community Discussion
No community discussion yet for this question.