I have the following command:

2>&1 compiler \
    --flag1 \
    --flag2 \
    --option X=10 \
    build \
    2>&1 | tee -a output.log

I'm not entirely sure what is the behavior, is it redundant and not needed?

I've used the redirection at the start to print to stderr

>&2 echo "Diagnostic Title"

I'm going over but struggling a bit and see no mention of start vs end of command spec:

Any help is appreciated!


1 Answer 1


Some things to keep in mind:

  • redirections can appear anywhere in the command
  • redirections are processed left-to-right (but only after the pipeline is set up)

Given these 2 points, we can see the first one redirects stderr to whatever stdout is currently pointing to. (Here, the pipeline.) Then the second one does exactly the same thing. Since stdout has not changed in between, the 2nd redirection is indeed redundant.

Also, this one is very important and is a fairly common source of questions here:

  • redirections are processed before the command is actually invoked:

    sed 'some sed program' myfile > myfile

    truncates myfile first and then launches sed, which has a zero-length input file to process.

The accepted answer to the related question that @Kusalananda points to goes into greater depth.

You must log in to answer this question.

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