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

Install Pip on Ubuntu 18.04: Python 3 and Python 2 Setup Guide

Pip is the official package manager for Python and the standard way to install libraries from…

16 hours ago

Install R on Ubuntu 18.04 from CRAN: Statistical Computing Setup

R is an open-source programming language and environment built for statistical computing and data visualization. It…

16 hours ago

Install Jenkins on Ubuntu 18.04: CI/CD Server Setup Guide

Jenkins is an open-source automation server that makes it easy to build CI/CD pipelines. Continuous integration…

16 hours ago

Install Android Studio on Ubuntu 18.04 with Snap and OpenJDK 8

Android Studio is the official IDE for Android development, built on JetBrains' IntelliJ IDEA platform. It…

16 hours ago

Install and Configure GitLab on Ubuntu 18.04 with Omnibus

GitLab is a web-based, open-source Git repository manager written in Ruby. It includes built-in tools for…

16 hours ago

Install Anaconda on Ubuntu 18.04: Python Data Science Setup Guide

Anaconda is the most widely used Python distribution for data science and machine learning. It bundles…

2 days ago