Shell Scripting / Input and Output in Shell Scripts

Using Pipes and Tee for Efficient Data Flow

In this tutorial, you'll learn how to use pipes and 'tee' in shell scripting for efficient data processing. These tools allow you to chain commands together and send output to mul…

Tutorial 4 of 5 5 resources in this section

Section overview

5 resources

Covers capturing user input and redirecting output in shell scripts.

Using Pipes and Tee for Efficient Data Flow

Introduction

This tutorial is designed to teach you how to use pipes (|) and tee in the Linux shell for efficient data processing. By the end of this tutorial, you'll understand how to chain commands together and output data to multiple places simultaneously.

You'll learn:
- What pipes and tee are
- How to use pipes to chain commands
- How to use tee to write to multiple streams

Prerequisites:
- Basic knowledge of Linux command line
- Access to a Linux terminal

Step-by-Step Guide

Pipes (|)

A pipe, represented by the | symbol, links the stdout (standard output) of one command to the stdin (standard input) of another. This allows for commands to be chained together.

Example:

ls | grep 'txt'

In this example, the ls command lists all files in the current directory. The output is then passed through the pipe to the grep command, which filters out only lines containing 'txt'.

Tee

The tee command is used to read from standard input and write to standard output and files.

Example:

echo 'Hello, World' | tee file1.txt

In this example, the echo command prints 'Hello, World', which is passed to the tee command. tee then writes this to file1.txt and to standard output.

Code Examples

  1. Chaining commands using a pipe:
# List all files and filter out .txt files
ls | grep '.txt'
  1. Using tee to write to a file and standard output:
# Print 'Hello, World' and write to file1.txt
echo 'Hello, World' | tee file1.txt
  1. Writing to multiple files with tee:
# Print 'Hello, World' and write to file1.txt and file2.txt
echo 'Hello, World' | tee file1.txt file2.txt
  1. Appending to a file with tee:
# Append 'Goodbye, World' to file1.txt
echo 'Goodbye, World' | tee -a file1.txt

Summary

In this tutorial, you've learned how to use pipes and tee to chain commands and write to multiple streams simultaneously.

Next steps for learning could include exploring other ways to manipulate streams in the Linux command line, such as redirection (> and <).

Practice Exercises

  1. Chain 3 commands together using pipes.
  2. Use tee to write to two files at once.
  3. Use tee to append a line of text to a file.

Solutions:
1. ls | grep '.txt' | wc -l - This will list files, filter out .txt files, and then count the number of .txt files.
2. echo 'Hello, World' | tee file1.txt file2.txt - This will write 'Hello, World' to both file1.txt and file2.txt.
3. echo 'Goodbye, World' | tee -a file1.txt - This will append 'Goodbye, World' to file1.txt.

For further practice, try to come up with your own chains of commands and experiment with different uses of tee.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

PDF Compressor

Reduce the size of PDF files without losing quality.

Use tool

Countdown Timer Generator

Create customizable countdown timers for websites.

Use tool

Random Number Generator

Generate random numbers between specified ranges.

Use tool

PDF to Word Converter

Convert PDF files to editable Word documents.

Use tool

Open Graph Preview Tool

Preview and test Open Graph meta tags for social media.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help