Here documents (<<EOF) in Bash Scripting - Time & Space Complexity
We want to understand how the time it takes to run a script with a here document changes as the input grows.
Specifically, how does the script's work increase when the here document content gets bigger?
Analyze the time complexity of the following code snippet.
cat <<EOF
Line 1
Line 2
Line 3
EOF
This script uses a here document to send multiple lines of text to the cat command, which prints them out.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Reading and printing each line from the here document.
- How many times: Once for each line inside the here document.
As the number of lines in the here document grows, the script reads and prints more lines.
| Input Size (n lines) | Approx. Operations |
|---|---|
| 10 | 10 reads and prints |
| 100 | 100 reads and prints |
| 1000 | 1000 reads and prints |
Pattern observation: The work grows directly with the number of lines; doubling lines doubles work.
Time Complexity: O(n)
This means the time to run the script grows linearly with the number of lines in the here document.
[X] Wrong: "The here document runs instantly no matter how many lines it has."
[OK] Correct: Each line must be read and processed, so more lines mean more work and more time.
Understanding how input size affects script time helps you write efficient scripts and explain your reasoning clearly in interviews.
"What if the here document content was piped to a command that processes lines twice? How would the time complexity change?"
Practice
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]
- Thinking it creates files automatically
- Confusing with background job control
- Assuming it defines functions
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 <
- Putting <
- Swapping EOF and <<
- Adding spaces incorrectly
cat <
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]
- Expecting $USER to print literally
- Thinking it causes syntax error
- Confusing EOF marker as output
cat <<'EOF' Hello $USER EOF
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]
- Thinking quotes cause syntax error
- Expecting variables to expand inside quoted EOF
- Believing EOF must be uppercase
Path is: $PATH
Which here document syntax should you use?
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]
- Not quoting EOF and expecting literal $PATH
- Using escape \$PATH inside unquoted EOF
- Using double quotes which still expand variables
Ready to prove it?
Pass the test and this pattern moves unknown
