0
0
Bash Scriptingscripting~20 mins

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

Choose your learning style9 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.