Here strings (<<<) in Bash Scripting - Time & Space Complexity
We want to understand how the time taken by a bash script using here strings changes as the input size grows.
Specifically, how does the script's work increase when the text inside the here string gets bigger?
Analyze the time complexity of the following code snippet.
read -r line1 <<< Hello
read -r line2 <<< World
read -r line3 <<< "This is a longer string"
# Print the lines
printf "%s\n" "$line1" "$line2" "$line3"
This script reads three strings using here strings and prints them. Each here string feeds a line of text to the read command.
- Primary operation: Each here string passes its entire text to the read command once.
- How many times: The read command runs three times, once per here string.
As the length of each here string grows, the time to process it grows roughly in direct proportion.
| Input Size (characters) | Approx. Operations |
|---|---|
| 10 | About 10 operations to read the string |
| 100 | About 100 operations to read the string |
| 1000 | About 1000 operations to read the string |
Pattern observation: The work grows linearly with the size of the input string.
Time Complexity: O(n)
This means the time to process the here string grows directly with the length of the string.
[X] Wrong: "Here strings run instantly no matter how big the input is."
[OK] Correct: The shell must still read and process every character in the here string, so bigger input takes more time.
Understanding how input size affects script commands like here strings helps you write efficient scripts and explain your reasoning clearly in real situations.
"What if we replaced here strings with reading from a file? How would the time complexity change?"
Practice
<<< do in bash scripting?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 CQuick Check:
Here strings = send input string [OK]
- Confusing input with output redirection
- Thinking it creates files
- Mixing with multiline comments
cat command?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 usescat <<<"hello world".Final Answer:
cat <<<"hello world" -> Option AQuick Check:
Quotes needed with here strings = cat <<<"hello world" [OK]
- Omitting quotes causing errors
- Using << instead of <<<
- Confusing input redirection with here strings
grep 'world' <<< "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 DQuick Check:
grep outputs matching lines = hello world [OK]
- Expecting only the matched word
- Confusing grep output with cut or awk
- Thinking here string changes grep behavior
read name <<
Solution
Step 1: Analyze the here string usage
The here string operator works withreadand does not require quotes for simple alphanumeric strings like John.Step 2: Check the given snippet
read name << correctly setsname=John(even with literal \n if present), andecho "Name is $name"outputs correctly. No syntax or runtime errors.Final Answer:
No error, script runs fine -> Option AQuick Check:
Here strings + read works without quotes for simple input [OK]
- Assuming read can't use here strings
- Thinking quotes are always required
- Misunderstanding echo syntax
line. Which command correctly uses a here string to do this?Solution
Step 1: Understand the goal
We want to count occurrences of 'error' in the variableline.Step 2: Analyze each option
grep -o 'error' <<< "$line" | wc -l uses here string to send$lineto 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 BQuick Check:
Use grep -o with here string to count matches [OK]
- Using grep -c which counts lines, not matches
- Redirecting input from variable as file
- Misordering pipes causing wrong output
Ready to prove it?
Pass the test and this pattern moves unknown
