As an exercise to fully understand file descriptor redirection, I'm trying to store a command's STDOUT and STDERRunderstand file descriptors in separate variablesthe context of shell redirection.
Thanks to improvements made inWhy can't I have this answercat read from FD 3, I can store STDERR:
{ err=$(exec 2>&1 >&3 3>&-; ls -ld /x /bin | tr o Z); } 3>&1
I'm tryingwhich is being written to also storeby ls's STDOUT:?
{ err=$(exec 2>&1 >&3; ls -ld /x /bin); exec 0<&3; out=$(cat); } 3>&1;
HoweverWhen try this, the cat still wants to read from my keyboard.
How do I get my desired output:
$ printf 'OUT: %s\nERR: %s\n' "$out" "$err"
OUT: lrwxrwxrwx 1 root root 7 Aug 22 15:44 /bin -> usr/bin/
ERR: ls: cannot access '/x': No such file or directory
If this can't be done, why not?
Differentiation: This question is about reading / writing to the same file descriptor, using the problem presented by Redirect STDERR and STDOUT to different variables without temporary files as an example.