More Bash answers

by Danny
Published 2 min read

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.

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.


_10
# Overwrite the file with both streams (works in any POSIX shell)
_10
./build.sh > build.log 2>&1
_10
_10
# Append both streams
_10
./build.sh >> build.log 2>&1
_10
_10
# Bash shorthand for the same two commands
_10
./build.sh &> build.log
_10
./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:


_10
# Correct: stdout goes to the file first, then stderr copies it
_10
command > out.log 2>&1
_10
_10
# Wrong: stderr copies the terminal, then stdout moves to the file
_10
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


_10
./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.


_10
./build.sh 2>&1 | tee build.log
_10
./build.sh 2>&1 | tee -a build.log
_10
_10
# Bash shorthand for 2>&1 |
_10
./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


_10
# Silence everything
_10
./noisy.sh > /dev/null 2>&1
_10
_10
# Hide normal output but keep errors
_10
./noisy.sh > /dev/null

Redirect a whole script from inside it

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


_10
#!/usr/bin/env bash
_10
exec >> /var/log/backup.log 2>&1
_10
_10
echo "Backup started"
_10
rsync -a /data/ /backup/

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


_10
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.
#

Was this resource helpful?