0
0
Bash Scriptingscripting~3 mins

Why Here strings (<<<) in Bash Scripting? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if you could feed text to commands as easily as whispering a secret?

The Scenario

Imagine you want to quickly send a small piece of text to a command in your terminal, like searching for a word or counting characters. Without shortcuts, you might have to create a file or use complicated commands just to pass that tiny text.

The Problem

Typing out extra commands or creating temporary files just wastes time and can cause mistakes. It feels like using a hammer to turn a screw--too much effort for something simple. This slows you down and clutters your workspace.

The Solution

Here strings let you feed a short string directly into a command's input with a simple, clean syntax. It's like whispering the text straight to the command without any detours, making your scripts shorter and easier to read.

Before vs After
Before
echo "hello" | grep h
After
grep h <<< "hello"
What It Enables

It makes passing small text snippets to commands quick, neat, and error-free, speeding up your scripting and command-line work.

Real Life Example

Suppose you want to check if a word exists in a short message without creating a file or typing multiple commands. Here strings let you do this instantly, like searching for 'error' in a log snippet you just typed.

Key Takeaways

Manual text input to commands can be slow and messy.

Here strings provide a simple way to send short text directly.

This makes scripts cleaner and faster to write.