0

I am newer in bash. I try to create script that summarizetwo numbers:

Here is script:

echo "the result is:" expr $1+$2

Here how I call the script:

./scr3 50 98

And here is result that I get after the script executed:

the result is: expr 50+98

While I get the the string with two summarized numbers I expect to get the summarize of two numbers.

My question is why I don't get the result of summarize of the two numbers?

Michael
  • 13,950
  • 57
  • 145
  • 288

1 Answers1

1

Why? Because echo prints its arguments, and you're passing expr as an argument.

A best-practice alternative would be:

echo "The result is: $(( $1 + $2 ))"

...though the a smaller change (albeit to very inefficient code; expr is an artifact of the 1970s, made irrelevant with the introduction of $(( )) in the 1992 POSIX sh standard, and should never be used in new development) is simply:

echo "The result is: $( expr "$1" + "$2" )"
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441