I am testing the answer here but when I use a C program instead of bash script, I couldn't get it to work properly.
This is the C program.
// tss.c
#include <stdio.h>
int main()
{
while (1)
{
fprintf(stdout, "stdout: sias\n");
fprintf(stderr, "stderr: cias\n");
sleep(5);
}
return 0;
}
This is the test result, where I only see stderr not stdout.
$ $ { tss 2>&1 1>&3 3>&- | sed -u 's/^/err: /'; } 3>&1 1>&2 | sed -u 's/^/out: /'
err: stderr: cias
err: stderr: cias
I then notice that I need to do "fflush(stdout)" in the program. But one thing I don't understand why it is not needed for stderr, and is there a way to get it to work without inserting fflush?