Bird
Raised Fist0
Bash Scriptingscripting~20 mins

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

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Here String 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 Bash script using here string?
Consider the following Bash script snippet. What will it print when executed?
Bash Scripting
read line <<< "Hello World"
echo "$line"
Aread: command not found
Bline
CHello World
DHello
Attempts:
2 left
💡 Hint
The here string sends the string as input to the read command.
💻 Command Output
intermediate
2:00remaining
What does this script output when using here string with a loop?
What will be printed by this Bash script?
Bash Scripting
while read word; do
  echo "$word"
done <<< "one two three"
Aone two three
Bone
C
one two
three
D
one
two
three
Attempts:
2 left
💡 Hint
The here string sends the entire string as a single line to the loop.
📝 Syntax
advanced
2:00remaining
Which option correctly uses a here string to count words?
Which of the following Bash commands correctly counts the number of words in the string "apple banana cherry" using a here string?
Awc -w < "apple banana cherry"
Bwc -w <<< "apple banana cherry"
Cwc -w << "apple banana cherry"
Dwc -w <<< apple banana cherry
Attempts:
2 left
💡 Hint
Here strings use <<< followed by a quoted string.
🔧 Debug
advanced
2:00remaining
Why does this script fail to assign the variable correctly?
What is the error in this Bash script and why does it not assign the variable 'var' as expected? var=$(read line <<< "test") echo "$var"
AThe read command does not output anything, so var is empty
BSyntax error: missing quotes around read
Cread command cannot be used with here strings
DThe variable 'line' is not declared
Attempts:
2 left
💡 Hint
The read command reads input but does not print it.
🚀 Application
expert
2:00remaining
How to use here string to pass multiple lines to a command?
You want to pass multiple lines of text to a command using a here string. Which option correctly does this in Bash?
A
grep 'pattern' &lt;&lt;&lt; line1
line2
line3
B
grep 'pattern' &lt;&lt;&lt; "line1
line2
line3"
C
grep 'pattern' &lt;&lt;&lt; 'line1
line2
line3'
Dgrep 'pattern' <<< $'line1\nline2\nline3'
Attempts:
2 left
💡 Hint
Use $'...' to interpret escape sequences inside a here string.

Practice

(1/5)
1. What does the here string operator <<< do in bash scripting?
easy
A. Redirects output to a file
B. Starts a multiline comment
C. Sends a short string as input to a command
D. Creates a new shell process

Solution

  1. Step 1: Understand the purpose of here strings

    Here strings use <<< to send a short string directly as input to a command without needing a file.
  2. Step 2: Compare with other redirections

    Unlike output redirection or comments, here strings provide input data inline.
  3. Final Answer:

    Sends a short string as input to a command -> Option C
  4. Quick Check:

    Here strings = send input string [OK]
Hint: Here strings send input text directly to commands [OK]
Common Mistakes:
  • Confusing input with output redirection
  • Thinking it creates files
  • Mixing with multiline comments
2. Which of the following is the correct syntax to use a here string to send the text "hello world" to the cat command?
easy
A. cat <<<"hello world"
B. cat << hello
C. cat < hello
D. cat <<< hello world

Solution

  1. Step 1: Identify correct here string syntax

    The here string operator is <<< followed by a quoted string to preserve spaces or special characters.
  2. Step 2: Check each option

    cat <<< hello world misses quotes, which can cause word splitting. cat << hello uses here document syntax. cat < hello is input redirection from a file. cat <<<"hello world" correctly uses cat <<<"hello world".
  3. Final Answer:

    cat <<<"hello world" -> Option A
  4. Quick Check:

    Quotes needed with here strings = cat <<<"hello world" [OK]
Hint: Use <<< with quotes for strings containing spaces [OK]
Common Mistakes:
  • Omitting quotes causing errors
  • Using << instead of <<<
  • Confusing input redirection with here strings
3. What is the output of the following command?
grep 'world' <<< "hello world"
medium
A. "
B. world
C. hello
D. hello world

Solution

  1. Step 1: Understand grep with here string input

    The here string sends "hello world" as input to grep. Grep searches for lines containing 'world'.
  2. Step 2: Determine grep output

    The entire line "hello world" contains 'world', so grep outputs the full line.
  3. Final Answer:

    hello world -> Option D
  4. Quick Check:

    grep outputs matching lines = hello world [OK]
Hint: grep prints full matching lines from input [OK]
Common Mistakes:
  • Expecting only the matched word
  • Confusing grep output with cut or awk
  • Thinking here string changes grep behavior
4. Identify the error in this script snippet:
read name <<
medium
A. No error, script runs fine
B. read command cannot use here strings
C. Missing quotes around John in here string
D. Incorrect use of echo syntax

Solution

  1. Step 1: Analyze the here string usage

    The here string operator works with read and does not require quotes for simple alphanumeric strings like John.
  2. Step 2: Check the given snippet

    read name << correctly sets name=John (even with literal \n if present), and echo "Name is $name" outputs correctly. No syntax or runtime errors.
  3. Final Answer:

    No error, script runs fine -> Option A
  4. Quick Check:

    Here strings + read works without quotes for simple input [OK]
Hint: Here strings with read work without quotes for simple input [OK]
Common Mistakes:
  • Assuming read can't use here strings
  • Thinking quotes are always required
  • Misunderstanding echo syntax
5. You want to count how many times the word "error" appears in a log line stored in a variable line. Which command correctly uses a here string to do this?
hard
A. grep 'error' < $line | wc -l
B. grep -o 'error' <<< "$line" | wc -l
C. echo $line | grep -c 'error'
D. wc -l <<< "$line" | grep 'error'

Solution

  1. Step 1: Understand the goal

    We want to count occurrences of 'error' in the variable line.
  2. Step 2: Analyze each option

    grep -o 'error' <<< "$line" | wc -l uses here string to send $line to grep with -o (outputs each match on a new line), then counts lines with wc -l, correctly counting occurrences.
    grep 'error' < $line | wc -l tries to redirect input from a file named by $line, which is wrong.
    echo $line | grep -c 'error' echoes line and uses grep -c, which counts matching lines, not occurrences.
    wc -l <<< "$line" | grep 'error' pipes wc -l output to grep, which is incorrect usage.
  3. Final Answer:

    grep -o 'error' <<< "$line" | wc -l -> Option B
  4. Quick Check:

    Use grep -o with here string to count matches [OK]
Hint: Use grep -o with <<< and wc -l to count matches [OK]
Common Mistakes:
  • Using grep -c which counts lines, not matches
  • Redirecting input from variable as file
  • Misordering pipes causing wrong output
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