0
0
Bash Scriptingscripting~15 mins

Here strings (<<<) in Bash Scripting - Mini Project: Build & Apply

Choose your learning style9 modes available
Using Here Strings (<<<) in Bash Scripting
📖 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 &lt;&lt;&lt; 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use echo "Word count: $word_count" to display the result.