Redirecting stdout+stderr such that both get written to a file while still outputting to stdout is simple enough:
cmd 2>&1 | tee output_file
But then now both stdout/stderr from cmd are coming on stdout. I'd like to write stdout+stderr to the same file (so ordering is preserved assuming cmd is single threaded) but then still be able to also separately redirect them, something like this:
some_magic_tee_variant combined_output cmd > >(command-expecting-stdout) 2> >(command-expecting-stderr)
So combined_output contains the both with order preserved, but the command-expecting-stdout only gets stdout and command-expecting-stderr only gets stderr. Basically, I want to log stdout+stderr while still allowing stdout and stderr to be separately redirected and piped. The problem with the tee approach is it globs them together. Is there a way to do this in bash/zsh?
1
and2
(stdout and stderr) at e.g./dev/tty17
and runs the command. It's not as though the shell had to poll for the command's output and forward that output to the TTY. – Hubblebubble1:
for the stdout lines and2:
for the stderr lines? I think the most nearly reasonable way to do it is to have a process analogous tonohup
which takes the command and arguments and runs it under supervision:magic_trick -- cmd -o whatever
. Themagic_trick
program would perform some minor miracles with ptys (pseudo-ttys) if its own output is going to a tty, because the behaviour ofstdout
andstderr
depends on whether the output is going to a terminal or a pipe or a file. Lot's of details TBS. – Stepdaughter