nerdexam
CompTIA

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.

GNU and Unix Commands

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)
  • A
    3% (2)
  • B
    75% (45)
  • C
    8% (5)
  • D
    12% (7)
  • E
    2% (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.

ADisplays a bash syntax error message.

Single quotes are valid bash syntax; no syntax error is produced.

BDisplays the contents of the file named $TEST if it exists.Correct

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.

CWaits for the user to enter text and then echos the text back.

cat with a filename argument reads that file, not standard input; it will not wait for user input.

DDisplays the contents of the file named inside the back quotes.

The command uses single quotes, not backticks; backticks would perform command substitution, but single quotes do not.

EDisplays the contents of the file named by the environment variable TEST.

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

#shell quoting#single quotes#variable expansion#cat command

Community Discussion

No community discussion yet for this question.

Full LX0-103 Practice