Understanding Bash Redirection: The Key Differences Between > and >> Operators


2 views

In Bash scripting, the > and >> operators are fundamental tools for output redirection. While they look similar, their behavior differs in crucial ways that every Linux/Unix programmer should understand.

Operator: Overwrite Mode /h2>

The single greater-than symbol (>) creates a new file or overwrites an existing file with the command's output:

# Example 1: Basic > usage
echo "First line" > output.txt
cat output.txt
# Output: First line

echo "New content" > output.txt
cat output.txt
# Output: New content (previous content erased)

> Operator: Append Mode /h2>

The double greater-than symbol (>>) appends output to an existing file or creates a new file if none exists:

# Example 2: Basic >> usage
echo "First line" >> log.txt
echo "Second line" >> log.txt
cat log.txt
# Output:
# First line
# Second line

Let's demonstrate both operators in a single script:

#!/bin/bash
# Clear any existing file
> differences.txt

# Using > (overwrites each time)
for i in {1..3}; do
    echo "Attempt $i with >" > differences.txt
done

# Using >> (appends each time)
for i in {1..3}; do
    echo "Attempt $i with >>" >> differences.txt
done

cat differences.txt
# Output:
# Attempt 3 with >
# Attempt 1 with >>
# Attempt 2 with >>
# Attempt 3 with >>

> is ideal when:

  • You need fresh output each time
  • Working with temporary files
  • Initializing log rotation

>> works best for:

  • Logging continuous events
  • Maintaining history
  • Accumulating results

You can combine both operators with other redirection features:

# Redirect stderr to file while appending stdout
command 2> errors.log >> output.log

# Redirect both stdout and stderr while appending
command &>> combined.log

1. The > operator will truncate a file even if the command produces no output:

# Creates empty file
> empty.txt

2. Using >> on a non-existent file behaves like > for the first write:

# Equivalent to > if file doesn't exist
echo "Initial content" >> newfile.txt

In Bash shell scripting, the > and >> operators are fundamental for output redirection, but they behave differently in file handling:

# > operator (overwrites file)
echo "First line" > output.txt

# >> operator (appends to file)
echo "Second line" >> output.txt

The > operator creates a new file or truncates (clears) an existing file before writing, while >> preserves existing content and adds new data at the end.

# Demonstration of overwrite behavior
date > timestamp.log  # Creates/overwrites timestamp.log
sleep 1
date > timestamp.log  # Overwrites previous content

# Demonstration of append behavior
date >> history.log   # Creates or appends to history.log
sleep 1
date >> history.log   # Adds new line without deleting old content

Use > when:

  • Creating fresh log files for each script run
  • Storing single-instance output
  • You want to ensure no old data remains

Use >> when:

  • Maintaining application logs over time
  • Recording command history
  • Collecting multiple script execution results

Both operators create the target file if it doesn't exist (with 644 permissions by default). The file descriptor 1 (stdout) is implied when not specified. For explicit redirection:

# These are equivalent
command > file
command 1> file

# Redirect stderr (fd 2)
command 2> error.log

Here's a practical cat script example demonstrating both operators:

#!/bin/bash

# Create/clear output file
echo "=== NEW SESSION ===" > session.log

# Append subsequent data
cat /etc/os-release >> session.log
echo "" >> session.log  # Add blank line
uname -a >> session.log

# View final result
cat session.log

This script shows how > initializes the log file while >> accumulates additional system information.

  • Accidentally using > when you meant >>, destroying previous logs
  • Assuming >> creates parent directories (it doesn't)
  • Not considering file permissions when redirecting