📖 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 (<<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
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
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
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
Hint
Run the script and check the terminal output matches the three lines exactly.
Practice
(1/5)
1. What is the main purpose of using a here document (<
easy
A. To provide multiple lines of input or text to a command easily
B. To create a new file automatically
C. To run commands in the background
D. To define a function in the script
Solution
Step 1: Understand the here document concept
A here document lets you send multiple lines of text or commands as input to a command in a script.
Step 2: Identify the correct purpose
It is not for creating files, running background commands, or defining functions, but for providing multiline input.
Final Answer:
To provide multiple lines of input or text to a command easily -> Option A
Quick Check:
Here document = multiline input [OK]
Hint: Here documents send blocks of text to commands [OK]
Common Mistakes:
Thinking it creates files automatically
Confusing with background job control
Assuming it defines functions
2. Which of the following is the correct syntax to start a here document in bash?
easy
A. command <
B. <
C. command EOF<<
D. << command EOF
Solution
Step 1: Recall the here document syntax
The correct syntax places the command first, then <
Step 2: Match the options
Only command <
Final Answer:
command < -> Option A
Quick Check:
Command before <
Hint: Command comes before <
Common Mistakes:
Putting <
Swapping EOF and <<
Adding spaces incorrectly
3. What will be the output of this script?
cat <
medium
A. Hello $USER
B. Hello (your username)
C. Syntax error
D. Hello EOF
Solution
Step 1: Understand variable expansion in here documents
Without quotes around EOF, variables like $USER are expanded to their values.
Step 2: Predict the output
The script prints Hello followed by your actual username, not the literal $USER.
Final Answer:
Hello (your username) -> Option B
Quick Check:
Unquoted EOF expands variables [OK]
Hint: Unquoted EOF expands variables inside [OK]
Common Mistakes:
Expecting $USER to print literally
Thinking it causes syntax error
Confusing EOF marker as output
4. Identify the error in this script snippet:
cat <<'EOF'
Hello $USER
EOF
medium
A. The EOF marker should not be quoted
B. EOF must be uppercase
C. Missing semicolon after <<'EOF'
D. Variables inside quoted EOF are not expanded, so $USER prints literally
Solution
Step 1: Understand quoting effect on here documents
Quoting the EOF marker disables variable expansion inside the here document.
Step 2: Identify the output behavior
Because of quotes, $USER prints literally, which may be unexpected.
Final Answer:
Variables inside quoted EOF are not expanded, so $USER prints literally -> Option D
Quick Check:
Quoted EOF disables variable expansion [OK]
Hint: Quoted EOF means no variable expansion [OK]
Common Mistakes:
Thinking quotes cause syntax error
Expecting variables to expand inside quoted EOF
Believing EOF must be uppercase
5. You want to create a script that sends this exact text to a file, including the literal string $PATH:
Path is: $PATH
Which here document syntax should you use?
hard
A. cat <<"EOF" > file.txt
Path is: $PATH
EOF
B. cat < file.txt
Path is: $PATH
EOF
C. cat <<'EOF' > file.txt
Path is: $PATH
EOF
D. cat < file.txt
Path is: \$PATH
EOF
Solution
Step 1: Understand how to prevent variable expansion
To keep $PATH literal, quote the EOF marker to disable expansion.
Step 2: Choose the correct syntax
Using <<'EOF' prevents $PATH from expanding, so the exact text is written.
Final Answer:
cat <<'EOF' > file.txt
Path is: $PATH
EOF -> Option C
Quick Check:
Quote EOF to keep variables literal [OK]
Hint: Quote EOF to keep variables literal in output [OK]
Common Mistakes:
Not quoting EOF and expecting literal $PATH
Using escape \$PATH inside unquoted EOF
Using double quotes which still expand variables
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.