Shell Scripting / Input and Output in Shell Scripts

Redirecting Input and Output to Files

This tutorial will teach you about input and output redirection in shell scripts. You'll learn how to direct the flow of data, allowing programs to take input from files and outpu…

Tutorial 3 of 5 5 resources in this section

Section overview

5 resources

Covers capturing user input and redirecting output in shell scripts.

Introduction

  • Goal: The aim of this tutorial is to equip you with the knowledge of input and output redirection in shell scripts. This includes learning how to direct the flow of data, which allows programs to take input from files and send output to other files.
  • Learning Outcomes: You will learn the syntax and usage of shell redirection operators, and how to use them in your shell scripts. You will also learn how to handle errors using redirection.
  • Prerequisites: This tutorial assumes that you have a basic understanding of shell scripts and are familiar with command line interfaces.

Step-by-Step Guide

Input Redirection

The < character is used for input redirection. It instructs the shell to read input from a file.

Example: command < file

This tells the shell to execute command on file.

Output Redirection

The > character is used for output redirection. It instructs the shell to send the output of a command to a file.

Example: command > file

This tells the shell to execute command and then write the output to file.

Error Redirection

The 2> character is used for error redirection. It instructs the shell to send the error output of a command to a file.

Example: command 2> file

This tells the shell to execute command and then write any error messages to file.

Append Operator

The >> operator is used to append the output of a command to a file instead of overwriting the file.

Example: command >> file

This tells the shell to execute command and then append the output to file.

Code Examples

# Input Redirection
sort < file.txt
# This command sorts the contents of file.txt.

# Output Redirection
ls -l > file.txt
# This command writes the long listing of the current directory to file.txt.

# Error Redirection
ls -l non_existent_directory 2> error.txt
# This command tries to list a non-existent directory and writes the error message to error.txt.

# Append Operator
echo "Hello, World!" >> greetings.txt
# This command appends "Hello, World!" to greetings.txt.

Summary

  • We learned about input and output redirection in shell scripts.
  • We learned how to use the <, >, 2>, and >> operators for redirection.
  • We saw examples of how to use these operators in shell scripts.

Next Steps

  • Practice writing shell scripts using the redirection operators.
  • Learn about pipes (|), which allow for chaining commands and redirecting output from one command as input to another.
  • Learn about file descriptors and how they're used in redirection.

Additional Resources

Practice Exercises

  1. Write a script that sorts the contents of a file and writes the sorted content to a new file.
  2. Write a script that counts the number of lines in a file and appends the count to another file.
  3. Write a script that tries to access a non-existent directory and writes the error message to a file.

Solutions

# Exercise 1
sort < file.txt > sorted_file.txt

# Exercise 2
wc -l < file.txt >> line_count.txt

# Exercise 3
ls -l non_existent_directory 2> error.txt

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

Random Number Generator

Generate random numbers between specified ranges.

Use tool

Case Converter

Convert text to uppercase, lowercase, sentence case, or title case.

Use tool

Base64 Encoder/Decoder

Encode and decode Base64 strings.

Use tool

Color Palette Generator

Generate color palettes from images.

Use tool

EXIF Data Viewer/Remover

View and remove metadata from image files.

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