0
0
Bash Scriptingscripting~15 mins

Here documents (<<EOF) in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Here Documents (<<EOF) in Bash Scripts
📖 Scenario: You are writing a bash script to create a simple text file with multiple lines of content. Instead of typing each line with separate echo commands, you will use a here document to write all lines at once.
🎯 Goal: Learn how to use a here document (&lt;&lt;EOF) in a bash script to write multiple lines of text into a file in one step.
📋 What You'll Learn
Create a bash variable with the filename
Use a here document to write exactly three lines of text into the file
Use the EOF marker to end the here document
Print the contents of the file to verify the output
💡 Why This Matters
🌍 Real World
Here documents are used in scripts to quickly create configuration files, emails, or multi-line commands without many echo statements.
💼 Career
Knowing here documents helps automate tasks efficiently, a key skill for system administrators, DevOps engineers, and anyone writing shell scripts.
Progress0 / 4 steps
1
Create a filename variable
Create a bash variable called filename and set it to the string output.txt.
Bash Scripting
Need a hint?

Use filename="output.txt" to create the variable.

2
Write multiple lines using a here document
Use a here document with <<EOF to write these exact three lines into the file named by filename:
1. Hello, this is line one.
2. This is line two.
3. And this is line three.
Use EOF to mark the end of the here document.
Bash Scripting
Need a hint?

Use cat <<EOF > "$filename" followed by the lines and then EOF on its own line.

3
Add a command to display the file contents
Add a command to print the contents of the file named by filename to the terminal using cat.
Bash Scripting
Need a hint?

Use cat "$filename" to display the file content.

4
Run the script and check output
Run the script and ensure the output matches exactly these three lines:
Hello, this is line one.
This is line two.
And this is line three.
Bash Scripting
Need a hint?

Run the script and check the terminal output matches the three lines exactly.