I'm trying to figure out why the code below doesn't work, and gives me an error Bad file descriptor. It is sort of a follow-up to this question as applies to the script I'm currently working on.Early on in the caller, exec 3>&1 is run, and nothing later explicitly changes it before the (generalized) function below is called like so:exec 3>&1...string=$(GetString)GetString looks like this:GetString(){    4>&1 1>&3    #save pipe end and change output back to caller's    controlvar=0    while ((controlvar != 1))    do            printf some stuff for the interactive user\n        read -p my prompt variable        if ValidationFunction $variable; controlvar=1;fi    #tests for valid input    done    exec 1>&4-    #change output back to pipe end    echo $variable}I'm getting the Bad file descriptor error on the second to last line.What's going on here? Note that I'm also not doing anything explicitly with fd/4 elsewhere in the script.
Redirection Error in Function
bash;shell;io redirection
There are several problems with your question.  The line 4>&1 1>&3Is missing an exec:exec 4>&1 1>&3And the line:exec 1>4&-Should read exec 1>&4-A simplified script should look as this:GetString(){    exec 4>&1 1>&3    #save pipe end and change output back to caller's    printf some stuff for the interactive user\n    sleep 3        exec >&4-         #change output back to pipe end    echo test value}exec 3>&1string=$(GetString)echo final value <$string>This script makes sense.Please edit the question to actually reproduce your problem.