Quantcast
Viewing latest article 2
Browse Latest Browse All 3

Answer by glenn jackman for Read / write to the same file descriptor with shell redirection

It seems like a basic limitation of the variable assignment syntax, and the side-effects of the shell spawning subshells. You can either capture stderr or stdout but not both: the other stream needs to redirected into file (perhaps a FIFO).

# a function for testing
your_command() { sh -c 'echo "this is stdout"; echo "this is stderr" >&2'; }

errfile=$(mktemp)
out=$( your_command 2>|"$errfile" )
err=$(< "$errfile")
rm "$errfile"

echo "out: $out"
echo "err: $err"

Viewing latest article 2
Browse Latest Browse All 3

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>