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.
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)- A12% (5)
- B7% (3)
- C2% (1)
- D76% (32)
- E2% (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.
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.
The full content line is 'Hello foobar', not just 'Hello'; the heredoc preserves the entire line including both words.
A heredoc supplies text to a command via stdin and does not create any file as a side effect.
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.
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
Community Discussion
No community discussion yet for this question.