1

I have an expression declared as a string and I want the output to be the result of the expression. I am trying to solve it using shell scripting. For example

expr="123+456" 

expecting output

579

Thanks in advance.

CuriousTechie
  • 413
  • 2
  • 8
  • 21

3 Answers3

1

I would suggest using bc

> echo "123+456" | bc
579

Hard to say more without more details about what you are trying to do

See this SO for more details about doing this only using bash

Community
  • 1
  • 1
saladi
  • 3,103
  • 6
  • 36
  • 61
1

If you're dealing only in integers, bash can do it:

(( foo=123+456 ))
echo $foo

returns: 579

gilez
  • 669
  • 3
  • 6
0

You need the arithmetic expransion syntax:

$ expr="123+456"
$ answer=$(($expr))
$ echo $answer
579
glenn jackman
  • 238,783
  • 38
  • 220
  • 352