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

groupdel Command in Linux: Remove a Group and Audit Files

The groupdel command in Linux removes a group from the system. It deletes the group's entry from /etc/group and /etc/gshadow,…

16 hours ago

wc Command in Linux: Count Lines, Words, Characters, and Bytes

The wc command in Linux counts lines, words, characters, and bytes in files or standard input. It…

16 hours ago

top Command in Linux: Monitor Processes and Resource Usage

The top command in Linux provides a real-time view of running processes and system resource usage. From…

16 hours ago

usermod Command in Linux: Modify User Accounts and Groups

The usermod command in Linux modifies existing user account attributes. You can use it to manage group…

16 hours ago

sort Command in Linux: Sort Text, Numbers, Columns, and More

The sort command in Linux reads lines from files or standard input and writes them to standard…

2 days ago

wall Command in Linux: Broadcast Messages to Logged-In Users

The wall command in Linux sends a message to the terminals of all currently logged-in users. The…

2 days ago