Bash Scripting — Reading and Writing Files

2 minute read

Chapter 17 — Reading and Writing Files

Welcome to Chapter 17! 🎉 This is where your scripts start doing real work — reading configuration files, writing reports, appending log entries, and handling temporary files safely.

File I/O is at the heart of almost every useful Bash script. Whether you’re parsing a CSV, generating a config file, or building a deployment log — you need to know how to read and write files the right way.


What You’ll Learn

# Topic What You’ll Learn
1 Reading Files cat, while read, mapfile, reading specific lines and fields
2 Writing Files > overwrite, heredocs, printf, atomic writes
3 Appending to Files >> append, logging patterns, log rotation, flock
4 Writing with tee tee, tee -a, multiple files, sudo tee, process substitution
5 Safe Temporary Files mktemp, temp directories, trap cleanup, custom templates
6 Practice Exercises 5 hands-on exercises — easy to challenging

Prerequisites

Before starting this chapter, make sure you’re comfortable with:

  • Variables and quoting — "$var" vs $var
  • Loops — for, while
  • Conditionals — if, [[ ]]
  • Redirections basics — >, >>, <
  • Pipes — cmd1 | cmd2

If any of these feel shaky, revisit the earlier chapters first. No shame in that — solid fundamentals make everything easier.


Why File I/O Matters

Almost every real-world script involves files:

Task File Operation
Parse a config file Read lines, split on =
Generate a report Write output to .txt or .html
Application logging Append timestamped entries
Write to system files tee with sudo
Data processing pipeline Temp files for intermediate data

Quick Preview

Here’s a taste of what you’ll build in this chapter:

#!/usr/bin/env bash
set -euo pipefail

# Read a config file and export variables
while IFS='=' read -r key value; do
    [[ "$key" =~ ^#.*$ || -z "$key" ]] && continue
    export "$key"="$value"
done < config.env

# Write a report using a heredoc
cat <<EOF > report.txt
=== System Report ===
Host: $(hostname)
Date: $(date)
User: $USER
Uptime: $(uptime -p 2>/dev/null || uptime)
EOF

echo "Report written to report.txt"

By the end of this chapter, patterns like this will feel second nature.


Tips for This Chapter

  1. Always quote your variables — "$file" not $file. File paths with spaces will bite you otherwise.
  2. Prefer while read over cat — for large files, line-by-line processing is memory-safe.
  3. Use set -euo pipefail — catch errors early, especially when writing to files.
  4. Clean up temp files — always pair mktemp with a trap cleanup.
  5. Test with small files first — verify your logic before pointing at a 10 GB log file.

What’s Next?

Start with Reading Files → to learn every way Bash can read file content — from simple cat to powerful mapfile arrays.

Let’s dive in! 🚀

Updated: