![`sh -c` 内的可移植临时文件描述符](https://linux22.com/image/222161/%60sh%20-c%60%20%E5%86%85%E7%9A%84%E5%8F%AF%E7%A7%BB%E6%A4%8D%E4%B8%B4%E6%97%B6%E6%96%87%E4%BB%B6%E6%8F%8F%E8%BF%B0%E7%AC%A6.png)
cat <(echo yes)
显示“是”。在 Bash 5.2.15 上运行它会sh -m
产生同样的结果。
然而在 Bash 4.4.20 上它会抛出一个错误:
sh -c "cat <(echo yes)"
sh: -c: line 0: syntax error near unexpected token `('
为什么会出现错误?
sh -c
除了运行sh
然后键入命令之外,还有其他方法吗?
答案1
当您以 身份运行 Bash 时sh
,它会以 POSIX 模式启动,这会禁用一些非标准功能,包括 Bash 5.1 之前版本中的进程替换。从CHANGES
发行版中的文件:
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.
事实上,它在最新版本中工作可能是它没有列在其中的原因有关 POSIX 模式的参考手册页。
请注意,在许多系统上,sh
根本不是 Bash。如果你想要 Bash,请运行bash
.