Question

I'm wondering if anyone can point me at resources dealing with the low(ish) level mechanics of how output rediction works in bash. Unfortunately, I've only been able to turn up pages and pages of the basic "> sends output to a file" guides, but nothing seems to go into more detail than that.

In particular, I'm facing a strange situation whereby using the append redirector (>>) on Cygwin sometimes seems to start overwriting the target from the beginning of the file, rather than appending from the end as expected. I'm not aware of any combination of commands that can be given from bash to do this deliberately, but I wanted to get a better understanding of how the reidrection is actually handled in order to try and debug this behaviour and figure out what might be causing it.

The actual output is from a Java program that outputs straightforward progress messages via System.out.println(), in case there could be a gotcha here I'm not aware of

Was it helpful?

Solution

I don't know if this is how it works in Cygwin, but usually this kind of thing uses:

  1. fork to create a new process.
  2. open to open the redirection file.
  3. dup2 to make STDIN, STDOUT, or STDERR identical to the opened file.
  4. exec to run the specified command, with the streams now redirected.

The difference between ">" and ">>" is usually handled by giving different flags to the open command; for ">", the file is merely opened, while for ">>" the file is opened in append mode (O_APPEND).

I doubt that Cygwin has made any significant changes to the original BASH source code, so I suspect that what you are experiencing may be related to Cygwin's implementation of these UNIX functions on WIN32.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top