What causes this bash syntax error?

This command works fine on the command line ...

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

But when I try to add it to my .bashrc as a function, it fails ...

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

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

What am I doing wrong here?

+4
source share
2 answers

When used { braces }in front of the closing bracket, you must have a new line or half-line. For single-line characters, this means you need a half-bell

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

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

+3
source

}not special; you need to explicitly close the previous command with ;if you put the function definition on one line.

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

Source: https://habr.com/ru/post/1693030/


All Articles