On 1/8/21 11:19 PM, Hyunho Cho wrote:

Bash Version: 5.0
Patch Level: 17
Release Status: release

####################################################################

i have tested below in gnome calculator, Qalculate, gawk, perl
and all results in -4 but bash is 4

$ awk 'BEGIN { print -2 ^ 2 }'
-4

$ perl -E 'say -2 ** 2'
-4

$ echo $(( -2 ** 2 ))                     # only bash results in 4
4

This is how bash does operator precedence.

Like C, unary plus and minus are operators that have higher precedence
than any of the mathematic operators (multiplication/division/addition/
subtraction/etc). So unary operators have a higher precedence than
exponentiation.

This behavior varies widely among programs that implement exponentiation
operators.

--
``The lyf so short, the craft so long to lerne.'' - Chaucer
                 ``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU    c...@case.edu    http://tiswww.cwru.edu/~chet/

Reply via email to