I am trying to execute a command which accumulates stdout
into an existing file and sends the error messages to another using the command below.
commmand >> /home/user/accumulate_output.log 2>& /home/user/error.log
which gives this error message
bash: /home/user/error.log :ambiguous redirect
What is the right syntax?
Best Answer
2>&1
means redirection of error stream to the standard output and&
character on its own doesn't have much meaning: it's waiting for you to provide a number of the file descriptor, but you are giving him a filename istead. You want to redirect into file, not a numbered file descriptor, soYou can understand
&1
and&2
as "filenames" referring to file descriptors of stdout and stderr. Now you see that&
in front of a filename doesn't make sense.In summary, the syntax is
n>&m
orn>file
wheren
is the file descriptor to redirect (if it is not specified, it means standard output,n=1
), and on the right side, you can either redirect to file descriptorm
, or a file with namefile
.m
can be a number or-
which means the file descriptor should be closed instead of redirected (redirected "nowhere").There is also a special syntax to redirect both stdout and stderr to the same place, but that's
&>
. It will just confuse you, as it doesn't fall into the regular syntax - it is a shortcut that actually performs two redirects at once.