Bash redirection: append to a file descriptor (2>>&1)

bashfile-descriptorsio-redirection

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

Best Answer

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

Related Question