5.4 Priority of conditional and arithmetic operations
Priority applies to the following operators that can be used in the let command:
-
Numeric value comparisons
-
Logical operators
-
Ternary operator
-
Arithmetic operations
The table below shows the priority of conditional expressions and arithmetic operations in descending order of priority level, where 1 is the highest priority. Operations are performed in descending order of the priority, starting from the highest.
Priority |
Operator |
---|---|
1 |
- (unary minus operator), !, ++, --, ~ |
2 |
** |
3 |
*, /, % |
4 |
+, - |
5 |
<<, >> |
6 |
<, <=, >, >= |
7 |
==, != |
8 |
& |
9 |
^ |
10 |
| |
11 |
&& |
12 |
|| |
13 |
?: (ternary operator) |
14 |
=, +=, -=, *=, /=, %=, &=, ^=, |=, <<=, >>= |
In the example shown below, 3**3 is calculated first because ** has a higher priority than *. As a result, the value 54 is assigned to a and 54 is output to the standard output.
let a=2*3**3 <-- 3 to the power of 3 is multiplied by 2 echo $a <-- 54 is output as the value of a to the standard output