Explaining heredocs in Bash — combined with stream redirection.

The aim of this page📝 is to explain initial confusion. Misunderstandings, when learning about heredocs simultaneously with stream redirection. Let’s look at a simple example of using a here document with the cat command to write to a file.

Pavol Kutaj
3 min readMar 21, 2024
cat > remrem.md << EOT
hello
world
EOT

This command creates a file named remrem.md with the contents hello and world written on separate lines.

  • Stream redirection operators in shell scripting, such as <, >, >>, and |, are used to manipulate input and output streams.
  • Stream redirection operators are executed in the left-to-right order.
  • The << operator, used in the example, is not considered a stream redirection operator; it is used to create a here document.
  • More on heredocs: https://medium.com/p/b64337f87bfc
  • More on redirection: https://medium.com/p/7e77ccb39a3f
  • In the provided example, the << EOT here document marker indicates the start of the here document.
  • The lines hello and world following the here document marker are the input for the cat command.
  • The output of the cat command, including the input lines, is redirected to the file remrem.md using the > operator.
  • The here document redirection (<< EOT) takes precedence over the output redirection (> remrem.md) in this example.
  • Placing the here document marker at the rightmost part of the command can be counterintuitive for those (like me) expecting a strict left-to-right structure.
  • The here document structure helps provide multiple lines of input to a command in a clear and readable manner.

ANKI

Question:
What does the `<<` operator do in shell scripting?
Answer:
The `<<` operator is used to create a here document in shell scripting.
Question:
What does the provided command `cat > remrem.md << EOT` achieve?
Answer:
This command creates a file named `remrem.md` with the contents `hello` and `world` written on separate lines.
Question:
Are here documents considered stream redirection operators in shell scripting?
Answer:
No, the `<<` operator used for here documents is not considered a stream redirection operator.
Question:
What is the order of processing stream redirection operators in shell scripting?
Answer:
Stream redirection operators are processed from left to right in shell scripting.
Question:
Why might the placement of the here document marker at the rightmost part of the command be counterintuitive?
Answer:
It can be counterintuitive for those expecting a strict left-to-right structure in command execution.
Question:
How can the here document structure help in shell scripting?
Answer:
The here document structure helps provide multiple lines of input to a command in a clear and readable manner.
Question:
What does the `>` operator do in shell scripting?
Answer:
The `>` operator is used for output redirection in shell scripting.
Question:
What does the `cat` command do in the provided example?
Answer:
The `cat` command reads input lines from the here document and writes them to the file `remrem.md`.
Question:
Why is understanding the order of stream redirection operators and here documents important in shell scripting?
Answer:
Understanding the order is essential for effectively manipulating input and output streams in shell scripts.
Question:
How can practicing with examples like the one provided enhance comprehension of shell scripting concepts?
Answer:
Practicing with examples can improve understanding of stream redirection and here documents in shell scripting.

--

--

Pavol Kutaj
Pavol Kutaj

Written by Pavol Kutaj

Today I Learnt | Infrastructure Support Engineer at snowplow.io with a passion for cloud infrastructure/terraform/python/docs. More at https://pavol.kutaj.com

No responses yet