2
In a bash script I'd like to execute a command while piping the stderr to both a file and to the terminal. However, I want stderr to stay on the '2' file descriptor so that I can parse it from an external process that is calling this bash script.
All the solutions I've seen so far involve swapping stdout with stderr and then tee'ing to another file, for example:
find /var/log 3>&1 1>&2 2>&3 | foo.file
If I am to call this script from another one, the stderr will go to stdout and stdout will go to stderr, which is no good for me. I've tried to swap them again like so:
find /var/log 3>&1 1>&2 2>&3 | foo.file 4>&1 1>&2 2>&4
but this doesn't work.
Should work, but I would expect stdout and stderr from
find
to desynchronize in the terminal sometimes. This is due totee
, buffers etc. Iffind
writes stdout and stderr directly to the same device, the issue never happens; but if the two streams are merged later, it's possible. – Kamil Maciorowski – 2018-09-25T10:40:21.980