0
0
Bash Scriptingscripting~10 mins

Here documents (<<EOF) in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Here documents (<<EOF)
Start script
Encounter <<EOF
Read lines until EOF
Pass lines as input
Command executes with input
Script continues or ends
The script reads lines after <<EOF until it finds EOF, then sends those lines as input to a command.
Execution Sample
Bash Scripting
cat <<EOF
Hello
World
EOF
This script sends the lines 'Hello' and 'World' as input to the cat command, which prints them.
Execution Table
StepActionInput ReadCommand InputOutput
1Start script
2Encounter <<EOF
3Read lineHello
4Read lineWorld
5Read lineEOF
6Pass lines to catHello\nWorldHello\nWorld
7cat prints inputHello World
8Script ends
💡 Reached EOF marker, input passed to command, script ends
Variable Tracker
VariableStartAfter Step 3After Step 4After Step 5Final
input_linesemptyHelloHello\nWorldHello\nWorldHello\nWorld
Key Moments - 3 Insights
Why does the script stop reading input after the EOF line?
Because the EOF marker tells the shell where the here document ends, as shown in execution_table step 5.
Are the lines inside the here document treated as commands?
No, they are treated as plain text input passed to the command before <<EOF, as seen in step 6.
What happens if the EOF marker is missing?
The shell waits indefinitely for the EOF marker, causing the script to hang, since it doesn't know where input ends.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the command input at step 6?
A"Hello\nWorld"
B"Hello"
C"World"
D"EOF"
💡 Hint
Check the 'Command Input' column at step 6 in the execution_table.
At which step does the script stop reading lines for the here document?
AStep 3
BStep 5
CStep 4
DStep 6
💡 Hint
Look at the 'Input Read' column to find when EOF is read in the execution_table.
If the EOF marker was changed to END, what would change in the execution table?
AThe output would be empty
BStep 6 would have no input
CStep 5 would read 'END' instead of 'EOF'
DThe script would end earlier
💡 Hint
The EOF marker defines the end line, so changing it changes the line read at step 5.
Concept Snapshot
Here documents (<<EOF) let you send multiple lines of text as input to a command.
Syntax: command <<EOF ... EOF
The shell reads lines until it finds EOF, then passes them to the command.
Useful for feeding scripts or commands with fixed input.
EOF marker can be any word but must match exactly.
Lines inside are treated as plain text, not commands.
Full Transcript
Here documents in bash scripting allow you to send multiple lines of text as input to a command. When the shell sees <<EOF, it starts reading the following lines until it finds a line with only EOF. These lines are then passed as input to the command before <<EOF. For example, 'cat <<EOF' followed by lines 'Hello' and 'World' and then 'EOF' will send 'Hello\nWorld' to cat, which prints them. The script stops reading input when it finds the EOF marker. If the EOF marker is missing, the shell waits forever. The lines inside the here document are not commands but plain text. You can change EOF to any word, but it must match exactly to end the input.