2

i am making a shell that will redirect < and > linux says redirection is handled left to right

but if this is true then the following would not work

1)tee newOutputFile1 < existingInputFile > newOutputFile2

according to the man page the input is first overwritten, then the output is overwritten if that is true, then by the time that newOutputFile2 gets set to stdout tee would have already started, and then newOutputFile2 would be wrong.

this is what i understand that it does

2)tee newOutputFile1 < existingInputFile > newOutputFile2

will first set newOutputfile2 as the stdout, and then set stdin as existingInputFile contents.

but if that is true then

3)tee newOutputFile1 > newOutputFile2 < existingInputFile

would execute the opposite and break things.

so when there is both < and > does it just assume first set stdout then set stdin?

1
  • 1
    "by the time that newOutputFile2 gets set to stdout, tee would have already started". Nope, there's your mistake. A fork happens to prevent messing up the streams of the main shell, then all the redirections take place in left-to-right order, (freopen), and the last step is to use exec to replace the fork child with the tee executable.
    – Ben Voigt
    Commented Feb 16, 2016 at 17:58

1 Answer 1

2

Redirection is resolved left-to-right but redirection is done before the program is executed. So in this case:

  1. existingInputFile is opened for reading and stdin is redirected.
  2. newOutputFile2 is opened for writing (possibly truncating it) and stdout is redirected.
  3. tee newOutputFile1 is executed. Only after that tee opens newOutputFile1 or uses stdin or stdout.

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