3

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

6

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

4

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

command &>>file

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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