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_10command > out.log 2>&1_10_10# Wrong: stderr copies the terminal, then stdout moves to the file_10command 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_10exec >> /var/log/backup.log 2>&1_10_10echo "Backup started"_10rsync -a /data/ /backup/
To log and still see the output, use process substitution with tee:
_10exec > >(tee -a backup.log) 2>&1
Common pitfalls
#- Putting
2>&1before> file. Errors still go to the terminal. - Using
&>in a script run bysh. Shells such as dash readcommand &> fileas "run command in the background" followed by an empty> file, which truncates the file. - Expecting
sudo echo "text" > /etc/fileto work. The redirection is done by your own shell beforesudoruns, so it fails with permission denied. Useecho "text" | sudo tee /etc/fileinstead. - Losing the exit code when piping to
tee. The pipeline reports the exit status oftee, not your command, unless you turn onset -o pipefail. - Overwriting a log you meant to keep. Use
>>, or turn onset -o noclobberso>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
#Was this resource helpful?