Portable temporary file descriptors inside `sh -c`

cat <(echo yes)

Displays "yes". And running this inside sh -m results in the same thing on Bash 5.2.15.

Yet on Bash 4.4.20 it throws an error:

sh -c "cat <(echo yes)"
sh: -c: line 0: syntax error near unexpected token `('

Why the error?

Are there any other ways sh -c differs from running sh and then typing the command?

Asked By: Zaz

||

When you run Bash as sh, it starts in POSIX mode, which disables some nonstandard features, including process substitution in versions before Bash 5.1. From the CHANGES file in the distribution:

This document details the changes between this version, bash-5.1-alpha, and
the previous version, bash-5.0-release.

3. New Features in Bash

u. Process substitution is now available in posix mode.

The fact that it works in the most recent versions is likely the reason it’s not listed in the reference manual page on POSIX mode.

Note that on many systems, sh is not Bash at all. If you want Bash, run bash.

Answered By: ilkkachu