Bash 脚本进程替换语法错误:"("unexpected [英] Bash script process substitution Syntax error: "(" unexpected

查看:47
本文介绍了Bash 脚本进程替换语法错误:"("unexpected的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想运行这个脚本:

#!/bin/bash
echo <(true)

我运行它:

sh file.sh

我得到 "Syntax error: "(" unexpected" .我发现了一些类似的情况,但仍然无法解决这个问题.我是 shell 脚本的初学者,但据我所知:

And I get "Syntax error: "(" unexpected" . I found some similar situations but still can't solve this. I'm a beginner at shell scripting , but as I understand:

  1. 我使用的shebang是正确的并且选择了bash shell,所以进程替换语法应该可以工作
  2. 我从命令行尝试了相同的方法,并且可以正常工作.我检查了 echo $0 ,它给了我 "bash" ,那么与在命令行中运行命令和调用相同 shell 的脚本有什么区别?
  1. the shebang I use is correct and chooses the bash shell , so the process substitution syntax should work
  2. I try the same from the command line and it works. I checked with echo $0 and it gives me "bash" , so what's the difference from running the command in the command line and from a script that invokes the same shell?

也许这很简单,但我找不到解释或解决方案.

Maybe it's something simple, but I couldn't find an explanation or solution.

推荐答案

您应该使用 bash 运行您的脚本,即 bash ./script.sh 或使使用将 ./script.sh 设置为可执行文件后的shebang.正如赛勒斯所评论的那样,只有使用 sh ./script.sh 运行它,我才会收到您的错误.

You should run your script with bash, i.e. either bash ./script.sh or making use of the shebang by ./script.sh after setting it to executable. Only running it with sh ./script.sh do I get your error, as commented by Cyrus.

另见:shebang 在 unix.SE 中的角色

这篇关于Bash 脚本进程替换语法错误:&quot;(&quot;unexpected的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆