0
0
Bash Scriptingscripting~10 mins

Here strings (<<<) in Bash Scripting - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Here strings (<<<)
Start script
Use <<< to send string
Command reads string as input
Command processes input
Output result
End script
The script uses <<< to send a string directly as input to a command, which then processes it and outputs the result.
Execution Sample
Bash Scripting
grep 'hello' <<< 'hello world'
echo $?
This code searches for 'hello' in the string 'hello world' using a here string and prints the exit status.
Execution Table
StepActionCommand InputCommand OutputExit Status
1Send 'hello world' to grep via here string'hello world'hello world0
2Print exit status of grep0
3End of script
💡 grep finds 'hello' in the input string, so exit status is 0 (success).
Variable Tracker
VariableStartAfter Step 1After Step 2Final
Command Input'hello world''hello world''hello world'
Exit Status000
Key Moments - 2 Insights
Why does grep get the string without using a file?
Because the here string (<<<) sends the string directly as input to grep, so no file is needed. See execution_table step 1.
What does the exit status 0 mean after grep runs?
Exit status 0 means grep found the pattern in the input. See execution_table step 2 where exit status is printed.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the command input to grep at step 1?
A'hello'
BEmpty string
C'hello world'
D'world hello'
💡 Hint
Check the 'Command Input' column in execution_table row for step 1.
At which step is the exit status of grep printed?
AStep 2
BStep 1
CStep 3
DIt is never printed
💡 Hint
Look at the 'Action' column in execution_table to find when exit status is printed.
If the string did not contain 'hello', what would the exit status be?
A0
B1
C2
DNo exit status
💡 Hint
Exit status 1 means grep did not find the pattern; see common grep exit codes.
Concept Snapshot
Here strings (<<<) send a string as input to a command.
Syntax: command <<< 'string'
The command reads the string as if from a file.
Useful for quick input without files.
Exit status shows if command succeeded.
Full Transcript
This example shows how to use a here string (<<<) in bash scripting. The here string sends the string 'hello world' directly as input to the grep command. Grep searches for the word 'hello' in this input. Since 'hello' is found, grep outputs the matching line and sets the exit status to 0, meaning success. Then the script prints the exit status. This method avoids creating a file for input and is a quick way to provide input to commands.