Bird
Raised Fist0
Bash Scriptingscripting~5 mins

Here documents (<<EOF) in Bash Scripting - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
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 <
Click to reveal answer
beginner
How do you start and end a here document in bash?
You start with <
Click to reveal answer
intermediate
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.
Click to reveal answer
beginner
Give a simple example of a here document that prints multiple lines using cat.
Example: cat <
Click to reveal answer
beginner
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.
Click to reveal answer
What does <
AStarts a here document to provide multi-line input
BEnds a script
CComments out lines
DRuns a command in the background
How do you correctly end a here document started with <
AWith a closing brace }
BWith a semicolon ;
CWith EOF on a line by itself, no spaces or tabs
DWith a comment #
What happens if you quote the EOF marker like <<'EOF'?
ANo variable or command expansion inside the here document
BThe here document is ignored
CThe script will fail
DVariables are expanded twice
Which command commonly uses here documents to read multi-line input?
Apwd
Bcat
Cgrep
Dls
Why use here documents instead of separate input files?
ATo avoid using variables
BBecause input files are not allowed
CTo speed up the computer
DTo keep all input inside the script for simplicity
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/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