10

I'm trying to append the output of a command (stdout and stderr) to an existing file.

What I'm trying to do is something like this:

command >>file 2>>&1

The problem is that 2>>&1 throws an error, but >>file 2>>file does not.

So, I think I'm misunderstanding how redirection works, or what is a file descriptor and what information is saved inside it.

Summarizing, what is the difference between the two following commands, and why the first one does not work, but the second one works?

command >>file 2>>&1      #not working
command >>file 2>>file    #working

Thanks

0

2 Answers 2

16

What you want to do is set up file descriptor 1 (stdout) to append to a file, then redirect fd 2 (stderr) to simply do what fd 1 is doing.

command >>file 2>&1

You get an error with 2>>&1 simply because >>& is not a redirection operator.

Read Redirections in the bash manual, particularly sections 3.6.5 and 3.6.8

11

Since this is tagged with bash, you can use &>> to redirect (appending) both stdout and stderr.

command &>>file

1
  • 1
    This seems to be bash 4+ - version 3.2.x (on MacOS Catalina) doesn't support it but 4.x on most current Linux does.
    – Ed Randall
    May 22, 2020 at 13:14

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .