linux - calc syntax error -


i looking simple command line calculator , found calc simple , useful , turns out installed on our server.

this site calc: http://www.isthe.com/chongo/tech/comp/calc/

however, when tried simple calculation like:

calc 3/(1+2) 

it gave me error message:

-bash: syntax error near unexpected token `(' 

however, works fine:

calc 3*(1+2)  3*(1+2) = 9.000000 

the server version is:

$ uname -sr linux 3.2.0-23-generic 

does have idea how error came up? thanks!

okay. here's what's going on.

this has nothing calc, , bash. explain this, i'm first going give example echo.

wlynch@host:/tmp/blah$ ls          # no files in directory wlynch@host:/tmp/blah$ echo *(3)   # works expect. *(3)                               # wlynch@host:/tmp/blah$ echo *(4)   # works expect. *(4)                               # wlynch@host:/tmp/blah$ touch 3     # create file named 3 in directory. wlynch@host:/tmp/blah$ ls          # 3                                  # wlynch@host:/tmp/blah$ echo *(3)   # gives different result! 3                                  # wlynch@host:/tmp/blah$ echo '*(3)' # quoting gives want. *(3)                               # wlynch@host:/tmp/blah$ echo *(4)   # works expected. *(4) 

note output has changed when created file in directory named 3.

what's going on here *(pattern) wildcard. it's matching count of contained pattern. bash has weird side effect, if pattern doesn't match, instead of returning nothing, returns pattern seen originally.

so in case, you're accidentally using 1 of bash's wildcard substitution methods. pattern matching.

now, why /() fails? that's because parentheses command in bash, , 1 happens being used syntax error. grouping commands.

the solution, mentioned in other answers, surround command line arguments quotes, none of these features enabled.


also, aside, can disable feature *(pattern) uses. if this, both /() , *() fail because of parentheses ():

wlynch@host:/tmp/blah$ shopt -s extglob wlynch@host:/tmp/blah$ echo *() *() wlynch@host:/tmp/blah$ echo /() bash: syntax error near unexpected token `('  wlynch@host:/tmp/blah$ shopt -u extglob wlynch@host:/tmp/blah$ echo *() bash: syntax error near unexpected token `(' wlynch@host:/tmp/blah$ echo /() bash: syntax error near unexpected token `(' 

Comments