Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

To redirect stdout to a truncated file in bash, i know to use:

cmd > file.txt

To redirect stdout in bash, appending to a file, i know to use:

cmd >> file.txt

To redirect both stdout and stderr to a truncated file, i know to use:

cmd &> file.txt

How do I redirect both stdout and stderr appending to a file? cmd &>> file.txt does not work for me.

share|improve this question
I would like to note that &>outfile is a Bash (and others) specific code and not portable. The way to go portable (similar to the appending answers) always was and still is >outfile 2>&1 –  TheBonsai May 18 '09 at 4:48

5 Answers 5

up vote 583 down vote accepted
cmd >>file.txt 2>&1
share|improve this answer
works great! but is there a way to make sense of this or should I treat this like an atomic bash construct? –  flybywire May 18 '09 at 8:15
It's simple redirection, redirection statements are evaluated, as always, from left to right. >>file : Red. STDOUT to file (append mode) (short for 1>>file) 2>&1 : Red. STDERR to "where stdout goes" Note that the interpretion "redirect STDERR to STDOUT" is wrong. –  TheBonsai May 18 '09 at 8:55
It says "append output (stdout, file descriptor 1) onto file.txt and send stderr (file descriptor 2) to the same place as fd1". –  Dennis Williamson May 18 '09 at 9:07
That even works in zsh. Thanks. –  Mark Jul 14 '09 at 21:09
if you do cmd >>file1 2>>file2 it should achieve what you want. –  Woodrow Douglass Sep 6 '13 at 21:24

There are two ways to do this, depending on your Bash version.

The classic and portable (Bash pre-4) way is:

cmd >> outfile 2>&1

A nonportable way, starting with Bash 4 is

cmd &>> outfile

(analog to &> outfile)

For good coding style, you should

  • decide if portability is a concern (then use classic way)
  • decide if portability even to Bash pre-4 is a concern (then use classic way)
  • no matter which syntax you use, not change it within the same script (confusion!)

If your script already starts with #!/bin/sh (no matter if intended or not), then the Bash 4 solution, and in general any Bash-specific code, is not the way to go.

Also remember that Bash 4 &>> is just shorter syntax — it does not introduce any new functionality or anything like that.

The syntax is (beside other redirection syntax) described here: http://bash-hackers.org/wiki/doku.php/syntax/redirection#appending_redirected_output_and_error_output

share|improve this answer
I prefer &>> as it's consistent with &> and >>. It's also easier to read 'append output and errors to this file' than 'send errors to output, append output to this file'. Note while Linux generally has a current version of bash, OS X, at the time of writing, still requires bash 4 to manually installed via homebrew etc. –  mikemaccana May 20 '13 at 9:30

In Bash 4 (as well as ZSH 4.3.11):

cmd &>>outfile

just out of box

share|improve this answer
Old post digging :/ (and answer is not to the point). –  AoeAoe Mar 27 '12 at 18:35
@AoeAoe: I would -1 if you could downvote comments. There is nothing wrong with digging up old posts if you have something valuable to add—the necromancer badge even encourages it (meta.stackexchange.com/questions/20524/reviving-old-questions). –  Mk12 Aug 25 '12 at 20:20
@AoeAoe: This actually works in Bash 4 too. –  Mk12 Sep 6 '12 at 21:11
@all: this is a good answer, since it works with bash and is brief, so I've edited to make sure it mentions bash explicitly. –  mikemaccana May 20 '13 at 8:47
@mikemaccana: TheBonsai's answer shows bash 4 solution since 2009 –  J.F. Sebastian Mar 27 '14 at 17:56

In Bash you can also explicitly specify your redirects to files:

cmd >log.out 2>log.out

This allows you to specify different files as well, if you wish.

cmd >log.out 2>log_error.out

Appending would be:

cmd >>log.out 2>>log_error.out
share|improve this answer

Try this

You_command 1>output.log  2>&1

Your usage of &>x.file does work in bash4. sorry for that : (

Here comes some additional tips.

0, 1, 2...9 are file descriptors in bash. 0 stands for stdin, 1 stands for stdout, 2 stands for stderror. 3~9 is spare for any other temporary usage.

Any file descriptor can be redirected to other file descriptor or file by using operator > or >>(append).

Usage: <file_descriptor> > <filename | &file_descriptor>

Please reference to http://www.tldp.org/LDP/abs/html/io-redirection.html

share|improve this answer
Your example will do something different than the OP asked for: It will redirect the stderr of You_command to stdout and the stdout of You_command to the file output.log. Additionally it will not append to the file but it will overwrite it. –  pabouk May 31 '14 at 12:38
Correct: File descriptor could be any values which is more than 3 for all other files. –  Itachi Dec 25 '14 at 6:46
Your answer shows the most common output redirection error: redirecting STDERR to where STDOUT is currently pointing and only after that redirecting STDOUT to file. This will not cause STDERR to be redirected to the same file. Order of the redirections matters. –  Jan Wikholm Jan 4 at 12:51
does it mean, i should firstly redirect STDERROR to STDOUT, then redirect STDOUT to a file. 1 > output.log 2>&1 –  Quintus.Zhou Mar 4 at 6:10
@Quintus.Zhou Yup. Your version redirects err to out, and at the same time out to file. –  Alex Yaroshevich Mar 8 at 23:22

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.