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

Bash Scripting Best Practices Every Beginner Should Know

Introduction Bash scripting is a powerful way to automate Linux tasks, but writing a script…

48 minutes ago

How To Create A Self-Signed SSL Certificate Using Bash And OpenSSL

Introduction A self-signed SSL certificate is a certificate that is created and signed by the…

2 hours ago

How To Debug Bash Scripts Using bash -x And set Commands

Introduction Debugging is an important part of Bash scripting. When a script does not work…

6 hours ago

How To Use Cron Jobs With Bash Scripts For Automation

Introduction Cron jobs are used in Linux to run commands or Bash scripts automatically at…

7 hours ago

How To Use grep, awk, And sed In Bash Scripts

Introduction The grep, awk, and sed commands are powerful text-processing tools in Linux. They are…

9 hours ago

How To Work With Files And Directories Using Bash Scripts

Introduction Working with files and directories is one of the most important skills in Bash…

10 hours ago