How To

How To Use Pipes In Bash Scripts For Command Chaining

Introduction

Pipes are an important feature in Linux and Bash scripting. A pipe allows you to send the output of one command as input to another command. This makes it easy to connect multiple commands together and create powerful one-line operations.

If you are learning Bash scripting, understanding pipes is very useful. Pipes are commonly used for text processing, log analysis, command filtering, report generation, and cybersecurity automation. Instead of saving output to a temporary file, you can pass it directly from one command to another.

What Is A Pipe In Bash?

A pipe is represented by the | symbol.

Basic syntax:

command1 | command2

Here, the output of command1 becomes the input of command2.

Example:

ls | grep ".txt"

This command lists files using ls and then filters only files containing .txt using grep.

Basic Pipe Example

Create some sample files:

touch file1.txt file2.log file3.txt

Now run:

ls | grep ".txt"

Output:

file1.txtfile3.txt

The ls command lists all files, and grep filters only the text files.

Using Pipes In Bash Scripts

Create a Bash script:

nano pipe-example.sh

Add the following code:

#!/bin/bashecho "Text files in current directory:"ls | grep ".txt"

Save and run it:

chmod +x pipe-example.sh./pipe-example.sh

This script displays only files that contain .txt in their name.

Chain Multiple Commands With Pipes

You can connect more than two commands using pipes.

Example:

cat /etc/passwd | grep "/bin/bash" | cut -d: -f1

This command reads /etc/passwd, finds users with /bin/bash, and prints only the usernames.

A better version without unnecessary cat is:

grep "/bin/bash" /etc/passwd | cut -d: -f1

Count Matching Lines Using Pipes

Pipes are useful for counting results.

ps aux | grep apache | wc -l

This checks running processes, filters lines containing apache, and counts them.

Another example:

ls *.txt 2>/dev/null | wc -l

This counts .txt files in the current directory.

Cybersecurity Example: Check Failed SSH Login Attempts

#!/bin/bashlog_file="/var/log/auth.log"echo "Top failed SSH login IPs:"grep "Failed password" "$log_file" | awk '{print $(NF-3)}' | sort | uniq -c | sort -nr | head

This script searches failed SSH login attempts, extracts IP addresses, counts them, sorts the results, and shows the top entries.

Using Pipes With tee Command

The tee command allows you to show output on the screen and save it to a file at the same time.

ls -la | tee file-list.txt

This displays the file list and saves it into file-list.txt.

Conclusion

Pipes in Bash are powerful for command chaining. They allow you to connect commands and process output step by step without creating temporary files.

For beginners, learning pipes is important because many real-world Bash scripts use them for filtering, counting, sorting, log analysis, and cybersecurity reporting. Once you understand pipes, you can write shorter, cleaner, and more powerful Bash scripts.

Cyber Defence

Recent Posts

git fetch vs git pull: How They Work and When to Use Each

Both git fetch and git pull talk to a remote repository, but they do very different things to your…

3 days ago

git cherry-pick Command: Apply Commits from Another Branch

Sometimes the change you need already exists, just on the wrong branch. A hotfix lands…

3 days ago

Best Email APIs for Secure Business Email: Why Developers Are Moving Beyond SMTP

Email is still one of the most important communication channels inside modern applications. Password resets,…

3 days ago

Nginx Commands in Linux: Start, Stop, Reload, Test, and Log

Nginx is a high-performance web server and reverse proxy trusted by some of the largest…

6 days ago

ufw Command in Linux: Manage Firewall Rules with Examples

ufw (Uncomplicated Firewall) sits on top of iptables (or nftables on newer systems) and replaces…

6 days ago

who Command in Linux: Show All Logged-In Users and Sessions

When you share a server with a team or investigate unexpected activity, the first question…

6 days ago