nerdexam
CompTIA

LX0-103 · Question #146

Which of the following is the output when the below shell script executes? cat <<foobar Hello foobar foobar

The correct answer is D. Hello foobar. A shell heredoc feeds all text between the opening delimiter token and the closing delimiter line to a command as standard input.

GNU and Unix Commands

Question

Which of the following is the output when the below shell script executes? cat <<foobar Hello foobar foobar

Options

  • AThe contents of the file foobar.
  • BHello
  • CNo output but a file named foobar is created.
  • DHello foobar
  • EHello foobar foobar

How the community answered

(42 responses)
  • A
    12% (5)
  • B
    7% (3)
  • C
    2% (1)
  • D
    76% (32)
  • E
    2% (1)

Why each option

A shell heredoc feeds all text between the opening delimiter token and the closing delimiter line to a command as standard input.

AThe contents of the file foobar.

cat with a heredoc reads from stdin provided by the shell, not from a file on disk; no file named 'foobar' is opened or read.

BHello

The full content line is 'Hello foobar', not just 'Hello'; the heredoc preserves the entire line including both words.

CNo output but a file named foobar is created.

A heredoc supplies text to a command via stdin and does not create any file as a side effect.

DHello foobarCorrect

The heredoc operator `<<foobar` instructs the shell to read all following lines as stdin until it encounters a line consisting solely of the delimiter token 'foobar'. The only content line before that closing token is 'Hello foobar', so cat receives and prints exactly that line. The closing 'foobar' token is consumed by the shell as the terminator and is never passed to cat.

EHello foobar foobar

The closing delimiter 'foobar' is consumed by the shell as the end-of-input marker and is not echoed to stdout, so it does not appear in the output.

Concept tested: Shell heredoc syntax and delimiter behavior

Source: https://www.gnu.org/software/bash/manual/bash.html#Here-Documents

Topics

#shell scripting#here documents#cat command#input redirection

Community Discussion

No community discussion yet for this question.

Full LX0-103 Practice