Skip to main content
Fixed formatting in example; tweaked wording.
Source Link

The mentioned solutions are fine for very simple calculations, but very error-prone. Examples:

# without spaces 20+5expr literally20+5 produces literally 20+5
expr 20+5
→ 20+5

# bc's result doesn't give the fractional part by default
bc <<<
  9.0/2.0
→ 4

# expr does only integer
expr 9 / 2
→ 4

# same for POSIX arithmetic expansion
echo $((9/2))
→ 4

# bash arithmetic expansion chokes on floats
echo $((9.0/2.0))
→ bash: 9/2.0: syntax error: invalid arithmetic operator (error token is ".0")

# Most `expr` implementations also have problems with floats
expr 9.0 / 2.0
→ expr: non-integer argument

A syntax error like the last ones never get unnoticedis easily noticed, but integer responses with a discarded float part can easily getgo unnoticed and lead to wrong results.

That's why I always use a scripting language like Lua for that. But you can choose any scripting language that you're familiar with. I just use Lua as an example. The advantages are

  • a familiar syntax
  • familiar functions
  • familiar caveats
  • flexible input
  • spaces usually don't matter
  • floating point output

Examples:

lua -e "print(9/2)"
→ 4.5

lua -e "print(9 / 2)"
→ 4.5

lua -e "print(9.0/2)"
→ 4.5

lua -e "print (9 /2.)"
→ 4.5

lua -e "print(math.sqrt(9))"
→ 3

The mentioned solutions are fine for very simple calculations, but very error-prone. Examples:

# without spaces 20+5 literally produces 20+5
expr 20+5
→ 20+5

# bc's result doesn't give the fractional part by default
bc <<<
 9.0/2.0
→ 4

# expr does only integer
expr 9 / 2
→ 4

# same for POSIX arithmetic expansion
echo $((9/2))
→ 4

# bash arithmetic expansion chokes on floats
echo $((9.0/2.0))
→ bash: 9/2.0: syntax error: invalid arithmetic operator (error token is ".0")

# Most `expr` implementations also have problems with floats
expr 9.0 / 2.0
→ expr: non-integer argument

A syntax error like the last ones never get unnoticed, but integer responses with a discarded float part can easily get unnoticed and lead to wrong results.

That's why I always use a scripting language like Lua for that. But you can choose any scripting language that you're familiar with. I just use Lua as an example. The advantages are

  • a familiar syntax
  • familiar functions
  • familiar caveats
  • flexible input
  • spaces usually don't matter
  • floating point output

Examples:

lua -e "print(9/2)"
→ 4.5

lua -e "print(9 / 2)"
→ 4.5

lua -e "print(9.0/2)"
→ 4.5

lua -e "print (9 /2.)"
→ 4.5

lua -e "print(math.sqrt(9))"
→ 3

The mentioned solutions are fine for very simple calculations, but very error-prone. Examples:

# without spaces expr 20+5 produces literally 20+5
expr 20+5
→ 20+5

# bc's result doesn't give the fractional part by default
bc <<< 9.0/2.0
→ 4

# expr does only integer
expr 9 / 2
→ 4

# same for POSIX arithmetic expansion
echo $((9/2))
→ 4

# bash arithmetic expansion chokes on floats
echo $((9.0/2.0))
→ bash: 9/2.0: syntax error: invalid arithmetic operator (error token is ".0")

# Most `expr` implementations also have problems with floats
expr 9.0 / 2.0
→ expr: non-integer argument

A syntax error like the last ones is easily noticed, but integer responses with a discarded float part can easily go unnoticed and lead to wrong results.

That's why I always use a scripting language like Lua for that. But you can choose any scripting language that you're familiar with. I just use Lua as an example. The advantages are

  • a familiar syntax
  • familiar functions
  • familiar caveats
  • flexible input
  • spaces usually don't matter
  • floating point output

Examples:

lua -e "print(9/2)"
→ 4.5

lua -e "print(9 / 2)"
→ 4.5

lua -e "print(9.0/2)"
→ 4.5

lua -e "print (9 /2.)"
→ 4.5

lua -e "print(math.sqrt(9))"
→ 3
nothing to do with echo
Source Link
Stéphane Chazelas
  • 586.8k
  • 96
  • 1.1k
  • 1.7k

The mentioned solutions are fine for very simple calculations, but very error-prone. Examples:

# without spaces 20+5 literally produces 20+5
expr 20+5
→ 20+5

# bc's result does'tdoesn't give the fractional part by default
bc <<<
9.0/2.0
→ 4

# expr does only integer
expr 9 / 2
→ 4

# same for echoPOSIX arithmetic expansion
echo $((9/2))
→ 4

# echobash arithmetic expansion chokes on floats
echo $((9.0/2.0))
→ bash: 9/2.0: syntax error: invalid arithmetic operator (error token is ".0")

# exprMost `expr` implementations also hashave problems with floats
expr 9.0 / 2.0
→ expr: non-integer argument

A syntax error like the last ones never get unnoticed, but integer responses with a discarded float part can easily get unnoticed and lead to wrong results.

That's why I always use a scripting language like Lua for that. But you can choose any scripting language that you're familiar with. I just use Lua as an example. The advantages are

  • a familiar syntax
  • familiar functions
  • familiar caveats
  • flexible input
  • spaces usually don't matter
  • floating point output

Examples:

lua -e "print(9/2)"
→ 4.5

lua -e "print(9 / 2)"
→ 4.5

lua -e "print(9.0/2)"
→ 4.5

lua -e "print (9 /2.)"
→ 4.5

lua -e "print(math.sqrt(9))"
→ 3

The mentioned solutions are fine for very simple calculations, but very error-prone. Examples:

# without spaces 20+5 literally produces 20+5
expr 20+5
→ 20+5

# bc's result does't give the fractional part by default
bc <<<
9.0/2.0
→ 4

# expr does only integer
expr 9 / 2
→ 4

# same for echo
echo $((9/2))
→ 4

# echo chokes on floats
echo $((9.0/2.0))
→ bash: 9/2.0: syntax error: invalid arithmetic operator (error token is ".0")

# expr also has problems with floats
expr 9.0 / 2.0
→ expr: non-integer argument

A syntax error like the last ones never get unnoticed, but integer responses with a discarded float part can easily get unnoticed and lead to wrong results.

That's why I always use a scripting language like Lua for that. But you can choose any scripting language that you're familiar with. I just use Lua as an example. The advantages are

  • a familiar syntax
  • familiar functions
  • familiar caveats
  • flexible input
  • spaces usually don't matter
  • floating point output

Examples:

lua -e "print(9/2)"
→ 4.5

lua -e "print(9 / 2)"
→ 4.5

lua -e "print(9.0/2)"
→ 4.5

lua -e "print (9 /2.)"
→ 4.5

lua -e "print(math.sqrt(9))"
→ 3

The mentioned solutions are fine for very simple calculations, but very error-prone. Examples:

# without spaces 20+5 literally produces 20+5
expr 20+5
→ 20+5

# bc's result doesn't give the fractional part by default
bc <<<
9.0/2.0
→ 4

# expr does only integer
expr 9 / 2
→ 4

# same for POSIX arithmetic expansion
echo $((9/2))
→ 4

# bash arithmetic expansion chokes on floats
echo $((9.0/2.0))
→ bash: 9/2.0: syntax error: invalid arithmetic operator (error token is ".0")

# Most `expr` implementations also have problems with floats
expr 9.0 / 2.0
→ expr: non-integer argument

A syntax error like the last ones never get unnoticed, but integer responses with a discarded float part can easily get unnoticed and lead to wrong results.

That's why I always use a scripting language like Lua for that. But you can choose any scripting language that you're familiar with. I just use Lua as an example. The advantages are

  • a familiar syntax
  • familiar functions
  • familiar caveats
  • flexible input
  • spaces usually don't matter
  • floating point output

Examples:

lua -e "print(9/2)"
→ 4.5

lua -e "print(9 / 2)"
→ 4.5

lua -e "print(9.0/2)"
→ 4.5

lua -e "print (9 /2.)"
→ 4.5

lua -e "print(math.sqrt(9))"
→ 3
Correct and improve examples
Source Link
Marco
  • 34.3k
  • 11
  • 116
  • 147

The mentioned solutions are fine for very simple calculations, but very error-prone. Examples:

# you needwithout spaces 20+5 literally produces 20+5
expr 20+5
→ 20+5

# bc's result isdoes't integergive the fractional part by default
bc <<<
9.0/2.0
→ 4

# expr does only integer
expr 9 / 2
→ 4

# same for echo
echo $((9/2))
→ 4

# echo chokes on floats
exprecho $((9.0/2.0))
→ bash: 9/2.0: syntax error: invalid arithmetic operator (error token is ".0")

# expr also has problems with floats
expr 9.0 / 2.0
→ expr: non-integer argument

A syntax error like the last oneones never getsget unnoticed, but integer responses with a discarded float part can easily get unnoticed and lead to wrong results.

That's why I always use a scripting language like Lua for that. But you can choose any scripting language that you're familiar with. I just use Lua as an example. The advantages are

  • a familiar syntax
  • familiar functions
  • familiar caveats
  • flexible input
  • spaces usually don't matter
  • floating point output

Examples:

lua -e "print(9/2)"
→ 4.5

lua -e "print(9 / 2)"
→ 4.5

lua -e "print(9.0/2)"
→ 4.5

lua -e "print (9 /2.)"
→ 4.5

lua -e "print(math.sqrt(9))"
→ 93

The mentioned solutions are fine for very simple calculations, but very error-prone. Examples:

# you need spaces
expr 20+5
→ 20+5

# bc's result is integer
bc <<<
9.0/2.0
→ 4

# expr chokes on floats
expr $((9.0/2.0))
→ bash: 9/2.0: syntax error: invalid arithmetic operator (error token is ".0")

A syntax error like the last one never gets unnoticed, but integer responses with a discarded float part can easily get unnoticed and lead to wrong results.

That's why I always use a scripting language like Lua for that. But you can choose any scripting language that you're familiar with. I just use Lua as an example. The advantages are

  • a familiar syntax
  • familiar functions
  • familiar caveats
  • flexible input
  • spaces usually don't matter
  • floating point output

Examples:

lua -e "print(9/2)"
→ 4.5

lua -e "print(9 / 2)"
→ 4.5

lua -e "print(9.0/2)"
→ 4.5

lua -e "print (9 /2.)"
→ 4.5

lua -e "print(math.sqrt(9))"
→ 9

The mentioned solutions are fine for very simple calculations, but very error-prone. Examples:

# without spaces 20+5 literally produces 20+5
expr 20+5
→ 20+5

# bc's result does't give the fractional part by default
bc <<<
9.0/2.0
→ 4

# expr does only integer
expr 9 / 2
→ 4

# same for echo
echo $((9/2))
→ 4

# echo chokes on floats
echo $((9.0/2.0))
→ bash: 9/2.0: syntax error: invalid arithmetic operator (error token is ".0")

# expr also has problems with floats
expr 9.0 / 2.0
→ expr: non-integer argument

A syntax error like the last ones never get unnoticed, but integer responses with a discarded float part can easily get unnoticed and lead to wrong results.

That's why I always use a scripting language like Lua for that. But you can choose any scripting language that you're familiar with. I just use Lua as an example. The advantages are

  • a familiar syntax
  • familiar functions
  • familiar caveats
  • flexible input
  • spaces usually don't matter
  • floating point output

Examples:

lua -e "print(9/2)"
→ 4.5

lua -e "print(9 / 2)"
→ 4.5

lua -e "print(9.0/2)"
→ 4.5

lua -e "print (9 /2.)"
→ 4.5

lua -e "print(math.sqrt(9))"
→ 3
Source Link
Marco
  • 34.3k
  • 11
  • 116
  • 147
Loading