Signal9Signal9 — home
online
back to labs

Here Documents: Multi-Line Input Inline

MediumCommand Line Guru10-15 min
The brief

A here document feeds several lines of input to a command without a separate file — everything between <<MARKER and a line containing only MARKER becomes the command's stdin.

Two tasks that turn on the ONE detail that trips everyone up — whether the marker is quoted. Put both in one script, /root/case/heredocs.sh, so the result can be re-run by anyone:

1. Write /root/out/motd.txt with cat and an UNQUOTED here-doc so the shell expands a variable inside it. The file must contain a line reading exactly:

Welcome, <the user running the script>

(produced by embedding $USER — as root that is "Welcome, root").

2. Write /root/out/template.sh with a QUOTED here-doc (<<'EOF') so the text is verbatim. It must contain the literal, UN-expanded line:

echo "Deploying as $USER"

Here the $USER must survive as literal text — which only a quoted marker guarantees.

Same tool, opposite behavior, decided entirely by the quotes on the marker. Grading runs your script as a different user and reads the files it writes, so the motd must really come from $USER.

What you'll do
  • Use an unquoted here-doc so $USER expands into /root/out/motd.txt
  • Use a quoted here-doc so $USER stays literal in /root/out/template.sh
  • Put both in /root/case/heredocs.sh so the result can be regenerated by any user

Hands-on lab: you work on a real Linux box in your browser, graded on the machine's actual state.