Search code examples
bashshellscriptingio-redirectionsubshell

Redirect sterr in subshell to stdout in current shell in bash script


I have a subshell carrying out a function:

local thing=$( doFunc )

doFunc sends logging output to stderr (2) and 'thing' gets assigned to doFunc's output on stdout (1).

How can I run this line, but print stderr from the subshell to stdout in the current shell?


Solution

  • You can first make a copy of stdout on another FD, then redirect to that like

    exec 3>&1
    local thing=$(doFunc 2>&3)