bash 和 ash 括号的区别

Difference on bash and ash parentheses

我尝试使用 diff 命令比较目录列表,它在 bash:

中工作正常
diff  <(cd alpha ; find . -type f) <(cd beta; find . -type f)

但是,在 ash 上(没有其他 shell 可用的嵌入式设备)我得到

-ash: syntax error: unexpected "("

读取输入运算符<或括号()有什么区别吗?

<(command) 语法是 Process Substitution 并且不受 ash shell(和其他 limited/etc.shell 的支持) .

不要将 <( … ) 中的尖括号与 cat < file 等重定向中的尖括号混淆。在 bash 中,<( echo hi ) 实际上是一个内容为 "hi" 的文件(至少为了阅读目的)。所以你可以做

$ cat < <( echo hi )
hi

你也可以

$ echo <( : )
/dev/fd/63

并且 shell 实际上将该进程替换扩展为文件名。

另一方面,

Process substitution is a bash feature. It is not part of the POSIX specification and does not exist in shells like ash. Redirection 是 POSIX.

我发现这是最简洁易懂的解决方案:

#!/bin/sh
diff /dev/fd/3 3<<-EOF /dev/fd/4 4<<-EOF
$(sort file1)
EOF
$(sort file2)
EOF

参考:https://unix.stackexchange.com/a/639752/137528