← Back to Bash Course | Chapter 3: Control Flow | Lesson 5 of 14

Redirections (> >> 2>)

In this page:

  1. Redirections (> >> 2>)

Redirections (> >> 2>)

> redirects standard output to a file, overwriting it; >> appends instead of overwriting. 2> redirects standard error separately from standard output, and 2>&1 merges error into the same stream as output. Redirection is essential for logging and separating normal output from error messages.

Warning: > truncates the target file immediately, even if the command that follows fails to run.

Example: Redirections (> >> 2>)

bash
#!/bin/bash
echo "First line" > output.txt
echo "Second line" >> output.txt
cat output.txt

ls /no/such/path 2> error.txt
cat error.txt

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.