# Descriptor plans for spawned commands: every arrangement here is handed # to spawn()/posix_spawn() as a map rather than performed in the shell. p() { sh -c "$1"; } sh -c 'echo out; echo err >&2' >o 2>e; cat o e sh -c 'echo out; echo err >&2' >oe 2>&1; cat oe sh -c 'echo out; echo err >&2' 2>&1 >o2 | sed 's/^/piped: /'; cat o2 sh -c 'echo out; echo err >&2' 3>&1 1>&2 2>&3 3>&- 2>/dev/null | sed 's/^/swapped: /' sh -c 'echo out; echo err >&2' 3>&2 2>&1 1>&3 2>/dev/null sh -c 'echo to3 >&3' 3>f3; cat f3 sh -c 'echo to9 >&9' 9>f9; cat f9 sh -c 'read x <&4; echo "fd4: $x"' 4&3' 3>&- 2>/dev/null || echo "fd 3 closed" sh -c 'echo x' >&- 2>/dev/null || echo "stdout closed" sh -c 'read x && echo "read $x"' <&- 2>/dev/null || echo "stdin closed" sh -c 'echo x >&2' 2>&- ; echo "stderr closed: $(($? != 0))" exec 6>f6; sh -c 'echo inherited >&6'; exec 6>&-; cat f6 exec 7f8; sh -c 'echo dup >&3' 3>&8; exec 8>&-; cat f8 sh -c 'echo a; echo b >&2' >same 2>same; wc -l over; sh -c 'echo second' >>over; cat over sh -c 'cat' copy; cat copy sh -c 'cat; echo e >&2' &1 | sort echo data | sh -c 'cat >&2' 2>&1 | sed 's/^/via stderr: /' echo data | sh -c 'cat <&3' 3<&0 sh -c 'echo x' 1>&2 2>/dev/null; echo "order matters: $?" sh -c 'echo visible' 2>/dev/null 1>&2; echo "(nothing above)" sh -c 'echo rw >&3; ' 3<>frw; cat frw i=0; while [ $i -lt 30 ]; do i=$((i+1)); sh -c : 3>f3 4>f9 5&1 >&9)" V=1 sh -c 'echo "$V"' >fv; cat fv; echo "[${V-unset}]"