Bird
Raised Fist0
Bash Scriptingscripting~20 mins

Here documents (<<EOF) in Bash Scripting - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Here Document Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 Command Output
intermediate
2:00remaining
What is the output of this here document script?
Consider the following Bash script using a here document. What will it print when run?
Bash Scripting
cat <<EOF
Hello
World
EOF
A
Hello
World
BHello World
Ccat: EOF: No such file or directory
DEOF\nHello\nWorld
Attempts:
2 left
💡 Hint
The here document sends the lines between <
💻 Command Output
intermediate
2:00remaining
What happens if the delimiter is quoted in a here document?
What will this script output? cat <<'EOF' $HOME EOF
Bash Scripting
cat <<'EOF'
$HOME
EOF
A/home/username
BSyntax error near unexpected token `EOF'
C$HOME
DEOF
Attempts:
2 left
💡 Hint
Quoting the delimiter disables variable expansion inside the here document.
🔧 Debug
advanced
2:00remaining
Why does this here document script fail to run?
This script is intended to print a multi-line message, but it fails with a syntax error. Identify the cause. cat <
Bash Scripting
cat <<EOF
Hello
World
Acat command is misspelled
BMissing closing delimiter 'EOF' causes syntax error
CHere document delimiter must be quoted
DEOF must be uppercase
Attempts:
2 left
💡 Hint
The shell expects the ending delimiter to close the here document.
🚀 Application
advanced
2:00remaining
Which script correctly uses a here document to create a file with variable content?
You want to create a file named 'greeting.txt' containing the text 'Hello, Alice!'. The variable NAME='Alice' is set. Which script does this correctly?
Bash Scripting
NAME='Alice'
# Choose the correct script
A
cat &lt;&lt;EOF &gt; greeting.txt
Hello, $NAME!
EOF
B
cat &lt;&lt;'EOF' &gt; greeting.txt
Hello, $NAME!
EOF
C
cat &lt;&lt;EOF &gt; greeting.txt
Hello, NAME!
EOF
D
cat &lt;&lt;EOF &gt; greeting.txt
Hello, \$NAME!
EOF
Attempts:
2 left
💡 Hint
Variable expansion happens only if the delimiter is unquoted.
🧠 Conceptual
expert
2:00remaining
How does the shell treat leading tabs in here documents with <<-?
What is the effect of using <<-EOF instead of <
Bash Scripting
cat <<-EOF
	Hello
	World
EOF
AThe here document is treated as a comment
BLeading spaces before content lines are stripped
CThe here document delimiter must be quoted
DLeading tabs before content lines are stripped
Attempts:
2 left
💡 Hint
The dash after << enables a special behavior with tabs.

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

  1. 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.
  2. Step 2: Identify the correct purpose

    It is not for creating files, running background commands, or defining functions, but for providing multiline input.
  3. Final Answer:

    To provide multiple lines of input or text to a command easily -> Option A
  4. 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

  1. Step 1: Recall the here document syntax

    The correct syntax places the command first, then <
  2. Step 2: Match the options

    Only command <
  3. Final Answer:

    command < -> Option A
  4. 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

  1. Step 1: Understand variable expansion in here documents

    Without quotes around EOF, variables like $USER are expanded to their values.
  2. Step 2: Predict the output

    The script prints Hello followed by your actual username, not the literal $USER.
  3. Final Answer:

    Hello (your username) -> Option B
  4. 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

  1. Step 1: Understand quoting effect on here documents

    Quoting the EOF marker disables variable expansion inside the here document.
  2. Step 2: Identify the output behavior

    Because of quotes, $USER prints literally, which may be unexpected.
  3. Final Answer:

    Variables inside quoted EOF are not expanded, so $USER prints literally -> Option D
  4. 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

  1. Step 1: Understand how to prevent variable expansion

    To keep $PATH literal, quote the EOF marker to disable expansion.
  2. Step 2: Choose the correct syntax

    Using <<'EOF' prevents $PATH from expanding, so the exact text is written.
  3. Final Answer:

    cat <<'EOF' > file.txt Path is: $PATH EOF -> Option C
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard