bash 脚本中临时无名管道的问题
我有以下问题:
当我直接在终端窗口中执行以下脚本时,命令的行为符合预期。
$ diff <(echo tmp) <(echo tmp1)
1c1
< tmp
---
> tmp1
但是,当我在 shell 脚本中编写相同的命令时,
#! /bin/bash
diff <(echo tmp) <(echo tmp1)
我收到以下错误消息:
$ sh test.sh
test.sh: line 2: syntax error near unexpected token `('
test.sh: line 2: ` diff <(echo tmp) <(echo tmp1)'
最初我认为这是 diff 的问题,但其他命令也会发生这种情况。有人知道导致问题的原因吗?
I have the following question:
When I execute the following script directly in a terminal window, the commands behave as expected.
$ diff <(echo tmp) <(echo tmp1)
1c1
< tmp
---
> tmp1
However when I write the same command in a shell script
#! /bin/bash
diff <(echo tmp) <(echo tmp1)
I get the following error message:
$ sh test.sh
test.sh: line 2: syntax error near unexpected token `('
test.sh: line 2: ` diff <(echo tmp) <(echo tmp1)'
Initially I thought this was an issue with diff, but this also happens with other commands. Does anybody have an idea what causes the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当我这样做时,尝试
或
工作对我来说效果很好。
看起来 bourne shell (sh) 不支持该语法。
Try
or
Works fine for me when I do either.
Looks like the syntax is not supported by the bourne shell (sh).
当使用
sh
调用 bash 时,它会以特殊的、符合 POSIX 的模式启动。这有不同的语法,我想这解释了不同的结果。请参阅 POSIX 模式的 bashref,#22:“进程替换不可用”。
When bash is invoked using
sh
, it starts up in a special, POSIX-compliant mode. This has different syntax, which I guess explains the different results.See bashref of POSIX mode, #22: "process substitution is not available".
这个语法看起来不太熟悉。您确定在终端中使用 bash 吗?您可以通过输入
echo $SHELL
进行验证。That syntax doesn't look familiar. Are you sure you are using bash in your terminal? You can verify by typing
echo $SHELL
.