Subshells! A simple test. $ cat > test.md << EOF > Just some text here > EOF $ msh -c "cat '$(echo test.md)'" Just some text here $ msh -c "'$(which echo)' hello" hello $ msh -c "`which echo` hello" hello Subshells like these must respect the current redirections in the shell too! DISABLED $ cat > test.sh << EOF > exec 6>"log" > f () { > echo hello > } > > pkgs="\$(f 1>&6)" > echo "Pkgs is: \$pkgs" > EOF $ sh test.sh Pkgs is: $ msh test.sh Pkgs is: A more complex example. DISABLED $ cat > test.sh << EOF > exec 4>&1 > exec >"log" > exec 2>&1 > > f () { > echo "f: \$1" > } > > if true; then > pkgs="\$(f hello 1>&6)" > fi 6>&1 > echo "Pkgs is empty \$pkgs" >&4 > EOF $ sh test.sh Pkgs is empty $ msh test.sh Pkgs is empty