linux - 为什么在 shell 脚本中使用 <() 会导致语法错误?

标签 linux bash shell

我正在编写一个名为 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 执行脚本,你有两个选择:

  1. 使用 bash 运行脚本:

    bash myShellScript.sh 
    
  2. 将脚本的第一行设置为#!/bin/bash (或者您系统中到 bash 的任何路径),然后像这样运行脚本:

    ./myShellScript.sh 
    

关于linux - 为什么在 shell 脚本中使用 <() 会导致语法错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25815861/

相关文章:

python - 你如何在 linux 上为 python 2.7 和 python 3.2 设置 virtualenv?

c - stat.h 在 Linux 中不可用?

linux - 在脚本文件中出现与 "set -e"相同的错误时停止 bash

bash - 在 bash 脚本的循环中使用 nohup

java - 无法获取 Runtime.getRuntime().exec(python script) 的输出;

c++ - g++ 不再查找标准库 OSX 10.6.8

c - Linux 中队列的 mq_receive 中的奇怪输出

json - 将变量传递给 aws cli 内联 json

shell - 如何在Unix中的文件的每一行末尾添加100个空格

c - 为什么我在父进程中使用了waitpid,但是子进程仍然在父进程之后运行?