Recall & Review
beginner Click to reveal answer
What is a here document (<
A here document lets you send multiple lines of text or commands as input to a program or command directly within a script. It uses <
beginner Click to reveal answer
How do you start and end a here document in bash?
You start with <
intermediate Click to reveal answer
What happens if you quote the EOF marker in a here document?
If you quote EOF like <<'EOF', the text inside is treated literally. Variables and commands inside won’t be expanded or executed.
beginner Click to reveal answer
Give a simple example of a here document that prints multiple lines using cat.
Example:
cat <
beginner Click to reveal answer
Why are here documents useful in scripting?
They let you write multi-line input or scripts inside your script without needing extra files. This makes scripts cleaner and easier to manage.What does <
✗ Incorrect
<
How do you correctly end a here document started with <
✗ Incorrect
The here document ends when the ending marker (EOF) appears alone on a line with no spaces or tabs.
What happens if you quote the EOF marker like <<'EOF'?
✗ Incorrect
Quoting EOF disables variable and command expansion inside the here document.
Which command commonly uses here documents to read multi-line input?
✗ Incorrect
cat can read multi-line input from a here document and print it.
Why use here documents instead of separate input files?
✗ Incorrect
Here documents keep multi-line input inside the script, making it easier to manage without extra files.
Explain how a here document works in bash scripting and give a simple example.
Think about how you send several lines to a command inside a script.
You got /4 concepts.
Describe the effect of quoting the EOF marker in a here document and why you might want to do that.
Consider when you want the text exactly as written.
You got /3 concepts.
Practice
1. What is the main purpose of using a here document (<
easy
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 AQuick 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
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 AQuick 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
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 BQuick 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
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 DQuick 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:
Which here document syntax should you use?
Path is: $PATH
Which here document syntax should you use?
hard
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 CQuick 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 unknown
10 questions ~5 mineasy hard
