如何区分shell脚本中两个命令的重定向

Pab*_*cia 2 linux bash shell command-line

在命令行中,我可以比较两个命令的输出

$ diff <(cmd1) <(cmd2)
Run Code Online (Sandbox Code Playgroud)

但如果我把它放在一个 shell 脚本中

$ sh do_two_comands_diff.sh
Run Code Online (Sandbox Code Playgroud)

这是行不通的

$ diff <(ls) <(ls -a)
0a1,2
> .
> ..

$ cat > test_diff.sh
diff <(ls) <(ls -a)
^D

$ sh test_diff.sh
test_diff.sh:1: test_diff.sh: Syntax error: "(" unexpected
Run Code Online (Sandbox Code Playgroud)

我尝试使用 $() 代替 () ,但没有成功。我知道在 shell 内部有时需要以与在控制台中不同的方式来完成,但在这里我没有任何线索。有人可以这么好心地将我指向正确的链接,以了解如何在 shell 中正确执行这些操作,以及为什么会发生这种情况?

P。

小智 5

确保在脚本上使用与命令行上使用的解释器相同的解释器

#!/bin/bash
Run Code Online (Sandbox Code Playgroud)