Skip to main content
Post Reopened by Tom Hale, Stéphane Chazelas shell
Differentiate question
Source Link
Tom Hale
  • 33.3k
  • 42
  • 163
  • 257

Understanding advanced Read / write to the same file descriptor redirectionswith shell redirection

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.

Understanding advanced file descriptor redirections

As an exercise to fully understand file descriptor redirection, I'm trying to store a command's STDOUT and STDERR in separate variables.

Thanks to improvements made in this answer, I can store STDERR:

{ err=$(exec 2>&1 >&3 3>&-; ls -ld /x /bin | tr o Z); } 3>&1

I'm trying to also store STDOUT:

{ err=$(exec 2>&1 >&3; ls -ld /x /bin); exec 0<&3; out=$(cat); } 3>&1;

However, 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?

Read / write to the same file descriptor with shell redirection

I'm trying to understand file descriptors in the context of shell redirection.

Why can't I have cat read from FD 3, which is being written to by ls's STDOUT?

{ err=$(exec 2>&1 >&3; ls -ld /x /bin); exec 0<&3; out=$(cat); } 3>&1;

When try this, cat still wants to read from my keyboard.

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.

Post Closed as "Duplicate" by glenn jackman shell-script
Source Link
Tom Hale
  • 33.3k
  • 42
  • 163
  • 257

Understanding advanced file descriptor redirections

As an exercise to fully understand file descriptor redirection, I'm trying to store a command's STDOUT and STDERR in separate variables.

Thanks to improvements made in this answer, I can store STDERR:

{ err=$(exec 2>&1 >&3 3>&-; ls -ld /x /bin | tr o Z); } 3>&1

I'm trying to also store STDOUT:

{ err=$(exec 2>&1 >&3; ls -ld /x /bin); exec 0<&3; out=$(cat); } 3>&1;

However, 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?