Have read the bash manual on duplicating file descriptors
Is there a way to redirect to multiple file descriptors. Say to both stdout
and stderr
. Like this?
echo "hello" >&1 >&2
This does work in zsh:
zsh -c 'echo "hello" >&1 >&2'
Is there a way to do this in bash?
1
Use tee
to split the output to multiple destinations. Then you can use /dev/stderr
make one of them stderr
(it writes to stdout
by default).
echo "hello" | tee /dev/stderr
More generally you can use /dev/fd/N
to write to file descriptor N.
Partial answer using tee
:
echo "hello" | tee /dev/fd/2
This outputs to both stdout(1) and stderr(2).
Issues:
- Doesn’t work in scripts.
- How to use other file descriptors than stdout(1)? Okay, tee can be chained.
echo "hello" | tee /dev/fd/2 | tee /dev/fd/N
. Thanks @Barmar
3