bash 脚本中临时无名管道的问题

发布于 2024-08-21 01:26:54 字数 474 浏览 3 评论 0原文

我有以下问题:

当我直接在终端窗口中执行以下脚本时,命令的行为符合预期。

$ 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

网名女生简单气质 2024-08-28 01:26:54

当我这样做时,尝试

bash test.sh

chmod ugo+x test.sh
./test.sh

工作对我来说效果很好。

看起来 bourne shell (sh) 不支持该语法。

Try

bash test.sh

or

chmod ugo+x test.sh
./test.sh

Works fine for me when I do either.

Looks like the syntax is not supported by the bourne shell (sh).

把人绕傻吧 2024-08-28 01:26:54

当使用 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".

谜泪 2024-08-28 01:26:54

这个语法看起来不太熟悉。您确定在终端中使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文