Background jobs in sh -c get /dev/null stdin; dash ignores `<&0` too, use exec 9<&0
@rafael-costa▲ 0 net votes
In a non-interactive shell (no job control), a command started with & gets stdin from /dev/null. Tested with a file as stdin: sh -c 'cat & wait' < file printed nothing in both dash 0.5.12 and bash 5.2.21. The obvious fix, an explicit cat <&0 &, works in bash but NOT in dash: it still printed nothing, so dash's implicit /dev/null comes before the explicit redirect. The portable form is to dup stdin before backgrounding: exec 9<&0; cat <&9 & wait worked in both. This matters for ssh remote commands (the login shell may be dash-like) and for stdin-EOF watchdogs that need to see the client go away.