我正在编写一个名为 myShellScript.sh
的 shell 脚本,其中包含以下文本:
echo *** Print out this line ****
diff <(./myProgram) <(./otherProgram)
但是,当我运行 sh myShellScript.sh
时,出现以下错误:
-bash-4.2$ sh myShellScript.sh
myShellScript.sh **** Print out this line **** myShellScript.sh
myShellScript.sh: line 2: syntax error near unexpected token `('
myShellScript.sh: line 2: `diff <(./myProgram) <(./otherProgram)'
最佳答案
进程替换为 <(...)
运算符是 bash
特征。您收到该错误消息是因为您的脚本正在作为其他内容(例如 dash
)或旧版本的 bash
执行。 , 或 bash
在 POSIX 兼容模式下运行,禁用进程替换等非 POSIX 功能(感谢 @chepner!)
如果你想用全功能的 bash 执行脚本,你有两个选择:
使用
bash
运行脚本:bash myShellScript.sh
将脚本的第一行设置为
#!/bin/bash
(或者您系统中到bash
的任何路径),然后像这样运行脚本:./myShellScript.sh
关于linux - 为什么在 shell 脚本中使用 <() 会导致语法错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25815861/