是什么导致此bash语法错误?

时间:2018-02-01 13:03:33

标签: bash

此命令在命令行上正常工作......

if g++ -std=c++11 main.cpp ; then ./a.out; fi

但是当我尝试将它作为函数添加到我的.bashrc时,它失败了......

function cgo() { if g++ -std=c++11 "$1" ; then ./a.out; fi }

>$ cgo main.cpp
bash: syntax error near unexpected token `main.cpp'

我在这里做错了什么?

2 个答案:

答案 0 :(得分:3)

}并不特别;如果将函数定义放在一行上,则需要使用;显式终止前面的命令。

function cgo () { if g++ -std=c++11 "$1"; then ./a.out; fi; }

答案 1 :(得分:3)

使用{ braces }时,您需要在闭括号之前使用换行符或分号。对于单行,这意味着你需要一个分号

function cgo() { if g++ -std=c++11 "$1" ; then ./a.out; fi; }
# ........................................................^

文档:https://www.gnu.org/software/bash/manual/bashref.html#Command-Grouping

相关问题