---
title: "How do I redirect stdout and stderr to a file in Bash?"
description: "Learn how to send a command's normal output and its errors to a file in Bash, append instead of overwriting, keep output on screen too, and why the order of redirections matters."
url: https://www.danny.engineering/answer/bash/how-do-i-redirect-stdout-and-stderr-to-a-file-in-bash
author: Danny
published: 2026-11-27T00:00:00.000Z
updated: 2026-11-27T18:57:00.000Z
tags: ["Bash"]
---

# How do I redirect stdout and stderr to a file in Bash?

## The short answer


Redirect standard output to the file, then point standard error at the same place with `2>&1`. Use `>>` instead of `>` to append rather than overwrite. In Bash you can also use the shorter `&>` and `&>>` forms.


```bash
# Overwrite the file with both streams (works in any POSIX shell)
./build.sh > build.log 2>&1

# Append both streams
./build.sh >> build.log 2>&1

# Bash shorthand for the same two commands
./build.sh &> build.log
./build.sh &>> build.log
```


## Why it happens


Every process starts with three open file descriptors: 0 is standard input, 1 is standard output (stdout) and 2 is standard error (stderr). Normal output goes to 1 and error messages go to 2. Both point at your terminal by default, which is why they look like one stream until you redirect one of them.


A plain `>` is short for `1>`, so it only moves stdout. Errors still appear on the screen and are missing from the file.


`2>&1` means "make descriptor 2 point to wherever descriptor 1 points right now". It copies the current target; it does not create a lasting link. Bash processes redirections from left to right, so the order matters:


```bash
# Correct: stdout goes to the file first, then stderr copies it
command > out.log 2>&1

# Wrong: stderr copies the terminal, then stdout moves to the file
command 2>&1 > out.log
```


In the second line, errors still go to the terminal, because at the moment `2>&1` ran, descriptor 1 was still the terminal.


## How to fix it


### Both streams to one file


Use `> file 2>&1` when the script might run under `sh` or another POSIX shell. Use `&> file` when you know it is Bash. `&>>` for appending needs Bash 4 or later, which matters on macOS where the system `/bin/bash` is still version 3.2.


### Each stream to its own file


```bash
./build.sh > build.out.log 2> build.err.log
```


### See the output and save it too


Pipe into `tee`, which writes to the screen and a file at the same time. Add `-a` to append.


```bash
./build.sh 2>&1 | tee build.log
./build.sh 2>&1 | tee -a build.log

# Bash shorthand for 2>&1 |
./build.sh |& tee build.log
```


Here `2>&1` comes before the pipe on purpose: the pipe is set up first, so descriptor 1 already points at it.


### Throw output away


```bash
# Silence everything
./noisy.sh > /dev/null 2>&1

# Hide normal output but keep errors
./noisy.sh > /dev/null
```


### Redirect a whole script from inside it


Use `exec` near the top of the script to redirect everything that follows.


```bash
#!/usr/bin/env bash
exec >> /var/log/backup.log 2>&1

echo "Backup started"
rsync -a /data/ /backup/
```


To log and still see the output, use process substitution with `tee`:


```bash
exec > >(tee -a backup.log) 2>&1
```


## Common pitfalls

- Putting `2>&1` before `> file`. Errors still go to the terminal.
- Using `&>` in a script run by `sh`. Shells such as dash read `command &> file` as "run command in the background" followed by an empty `> file`, which truncates the file.
- Expecting `sudo echo "text" > /etc/file` to work. The redirection is done by your own shell before `sudo` runs, so it fails with permission denied. Use `echo "text" | sudo tee /etc/file` instead.
- Losing the exit code when piping to `tee`. The pipeline reports the exit status of `tee`, not your command, unless you turn on `set -o pipefail`.
- Overwriting a log you meant to keep. Use `>>`, or turn on `set -o noclobber` so `>` refuses to overwrite existing files.
- Output appearing out of order in the file. Some programs buffer stdout but not stderr, so lines from the two streams can interleave differently from what you saw in the terminal.

## Related reading

- [GNU Bash manual: Redirections](https://www.gnu.org/software/bash/manual/html_node/Redirections.html)
- [GNU Bash manual: Pipelines](https://www.gnu.org/software/bash/manual/html_node/Pipelines.html)
