0

I'm noticing an error:

bash: syntax error near unexpected token `-105.5*7+50*3'

When executing the below script/expression:

expr (-105.5*7+50*3)/20 + (19^2)/7 | bc -l

Is there any other way to evaluate such mathematical/floating point operations?

EDIT #1

NOTE: echo in place of expr does resolve this however I've used expr with bc before and it has handled floats quite normally why not in this scenario is what I'd like to find out now.

11
  • 5
    Replace expr with echo and put quotes around the expression.
    – wurtel
    Oct 31, 2014 at 10:08
  • 1
    @user2546040 You replaced expr with echo, but you forgot the second part. Quote the expression.
    – geirha
    Oct 31, 2014 at 10:18
  • 4
    expr is a command that can also perform (basic integer) arithmetic, but then it's pointless to pipe its output to bc -l which also does mathematics. Choose one or the other, not both. Using echo sends the expression to the standard input of bc -l which then evaluates the expression.
    – wurtel
    Oct 31, 2014 at 10:34
  • 2
    @user2546040 - you cannot use expr in this scenario b/c the expressions involves floats. expr can only deal with integer or strings.
    – slm
    Oct 31, 2014 at 11:46
  • 1
    @wrutel & slm thank you both I always thought piping with bc provided expr with that functionality now i am convinced thank you all for your suggestions
    – snoopy
    Oct 31, 2014 at 15:54

1 Answer 1

1

Is it possible that earlier you placed quotes around expr parameters, like this:

$ expr '(-105.5*7+50*3)/20 + (19^2)/7' | bc -l
22.14642857142857142857

?

In this case expr doesn't perform any arithmetic operations and just prints the original expression:

$ expr '(-105.5*7+50*3)/20 + (19^2)/7'        
(-105.5*7+50*3)/20 + (19^2)/7

All calculations happen in the bc -l, and the whole command gives the right answer (although expr actually works as echo).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .