📖 Scenario: You are writing a small bash script to process a short text input without creating a separate file. You want to use a simple way to send a string directly to a command.
🎯 Goal: Learn how to use the here string operator <<< to pass a string as input to a command in bash.
📋 What You'll Learn
Create a variable with a short text string
Use a here string to send the variable content to the wc -w command to count words
Store the word count in a variable
Print the word count
💡 Why This Matters
🌍 Real World
Here strings let you quickly send small pieces of text to commands without creating files. This is useful in scripts that process dynamic or short inputs.
💼 Career
Many automation and scripting tasks require passing data to commands efficiently. Knowing here strings helps write cleaner, faster bash scripts.
Progress0 / 4 steps
1
Create a variable with a text string
Create a variable called text and set it to the string "Hello world from bash scripting" exactly.
Bash Scripting
Hint
Use = to assign the string to the variable text. Enclose the string in double quotes.
2
Use a here string to count words
Create a variable called word_count and set it to the output of the command wc -w using a here string with the variable text as input.
Bash Scripting
Hint
Use $(command) to capture command output. Use wc -w <<< "$text" to count words from the variable.
3
Trim whitespace from word count
Update the word_count assignment to remove any leading or trailing whitespace from the output using tr -d ' '.
Bash Scripting
Hint
Pipe the output of wc -w to tr -d ' ' to delete spaces.
4
Print the word count
Write a echo command to print the text Word count: followed by the value of the variable word_count.
Bash Scripting
Hint
Use echo "Word count: $word_count" to display the result.
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
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.
Step 2: Compare with other redirections
Unlike output redirection or comments, here strings provide input data inline.
Final Answer:
Sends a short string as input to a command -> Option C
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
Step 1: Identify correct here string syntax
The here string operator is <<< followed by a quoted string to preserve spaces or special characters.
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".
Final Answer:
cat <<<"hello world" -> Option A
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
Step 1: Understand grep with here string input
The here string sends "hello world" as input to grep. Grep searches for lines containing 'world'.
Step 2: Determine grep output
The entire line "hello world" contains 'world', so grep outputs the full line.
Final Answer:
hello world -> Option D
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
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.
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.
Final Answer:
No error, script runs fine -> Option A
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
Step 1: Understand the goal
We want to count occurrences of 'error' in the variable line.
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.
Final Answer:
grep -o 'error' <<< "$line" | wc -l -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.