Shell Scripting / File Handling in Shell Scripts
Reading and Writing to Files Using Redirection
This tutorial explains how to read from and write to files using redirection in shell scripts. Redirection is a powerful feature that allows data flow to be controlled, enabling t…
Section overview
5 resourcesFocuses on reading, writing, and manipulating files within shell scripts.
Reading and Writing to Files Using Redirection
Introduction
This tutorial aims to teach you how to read from and write to files using redirection in shell scripts. Redirection is a powerful feature that allows data flow to be controlled. It enables the input and output of a program to come from and go to other programs or files.
By the end of this tutorial, you will be able to:
- Understand the concept of redirection in shell scripts
- Read from and write to files using redirection
- Apply best practices when using redirection
Prerequisites: Basic knowledge of shell scripting would be beneficial, but it's not mandatory.
Step-by-Step Guide
In shell scripts, there are three standard streams: Standard Input (stdin), Standard Output (stdout), and Standard Error (stderr). By default, stdin reads input from the keyboard, stdout prints output to the screen, and stderr prints error messages to the screen. Redirection allows you to change these defaults.
- Output Redirection: The '>' operator is used to redirect the output of a command to a file. If the file does not exist, it will be created. If it does exist, it will be overwritten.
Example:
echo "Hello, World!" > hello.txt
This command writes "Hello, World!" to the file hello.txt.
- Input Redirection: The '<' operator is used to take input from a file rather than the keyboard.
Example:
sort < file.txt
This command sorts the lines in file.txt.
- Appending to a file: The '>>' operator is used to append the output to an existing file.
Example:
echo "This is a new line" >> file.txt
This command adds "This is a new line" to the end of file.txt.
Code Examples
- Writing to a file
# This command writes "Hello, World!" to hello.txt
echo "Hello, World!" > hello.txt
- Reading from a file
# This command sorts the lines in file.txt
sort < file.txt
- Appending to a file
# This command adds "This is a new line" to the end of file.txt
echo "This is a new line" >> file.txt
Summary
In this tutorial, we covered the concept of redirection in shell scripts. We learned how to read from and write to files using the '<' and '>' operators, respectively, and how to append to a file using '>>'.
Next, you could learn about pipes, which is another powerful feature in shell scripting that works well with redirection.
Additional resources:
- Learn Shell
- Shell Scripting Tutorial
Practice Exercises
- Exercise 1: Write a command to redirect the output of the 'ls' command to a file named dir.txt.
- Solution:
ls > dir.txt -
This command writes the output of 'ls' to dir.txt.
-
Exercise 2: Write a command to append the current date to a file named date.txt.
- Solution:
date >> date.txt -
This command adds the current date to the end of date.txt.
-
Exercise 3: Write a command to sort the lines in a file named unsorted.txt and write the sorted lines to a file named sorted.txt.
- Solution:
sort < unsorted.txt > sorted.txt - This command sorts the lines in unsorted.txt and writes the sorted lines to sorted.txt.
Practice these exercises and modify them to get a better understanding of redirection. Happy learning!
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI 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 articleAI 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 articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article