The following sections contain examples for each of the arithmetic operators available to the Korn shell. Table 6.1 lists all of the arithmetic operators available to the Korn shell in order of precedence.
Evaluates to the negative value of expression.
$ ((X=—7)) $ ((Y=—X + 2)) $ print — "$X $Y" —7 9
The print – command is used so that the negative sign is not interpreted as an argument.
The ! operator returns 0 (true) for expressions that do not evaluate to zero, or 1 (false) if they do.
$ X=0 $ ((X=!X)); print "$X" 1 $ ((X=!X)); print "$X" 0
Evaluates to the bitwise negated value (one's complement) of expression. It returns a value that contains a 1 in each bit position where expression contains 0, and a 0 in each bit position where expression contains 1.
$ X=2#011 $ ((X=∼X)); print — "$X" —2#100
Evaluates to the product of expression1 multiplied by expression2. The second format assigned the result of the evaluation to expression1.
$ ((X=5 * 4)); print "$X" 20 $ ((X=3 * 1 * 2 * 4)); print "$X" 24 $ ((X*=2)); print "$X" 48
Evaluates to the quotient of expression1 divided by expression2. The second format assigned the result of the evaluation to expression1.
$ Y=50 $ ((X=Y / 10)); print "$X" 5 $ ((X=21 / 5)); print "$X" 4 $ ((X/=2)); print "$X" 2
Evaluates to the remainder of expression1 divided by expression2. The second format assigned the result of the evaluation to expression1.
$ ((X=20 % 7)); print "$X" 6 $ ((X=11 % 4)); print "$X" 3 $ ((X%=2)); print "$X" 1
Evaluates to the sum of expression1 and expression2. The second format assigned the result of the evaluation to expression1.
$ ((X=1 + 2)); print "$X" 3 $ ((X=4 + 1 + 3)); print "$X" 8 $ ((X+=1)); print "$X" 9
Evaluates to the difference of expression1 and expression2. The second format assigned the result of the evaluation to expression1.
$ ((X=3 — 1)); print "$X" 2 $ ((X=X — 1)); print "$X" 1 $ ((X—=1)); print "$X" 0
Assigns identifier the value of expression.
$ ((X=12)); print "$X" 12 $ Y=7 $ ((X=Y)); print "$X" 2
Left shift expression1 by the number of bits specified in expression2. The second format assigned the result of the evaluation to expression1.
$ typeset —i2 X $ ((X=2#11 << 1)); print "$X" 2#110 $ ((X=2#110 << 2)); print "$X" 2#11000 $ ((X=2#11000 << 3)); print "$X" 2#11000000
Right shift expression1 by the number of bits from expression2. The second format assigned the result of the evaluation to expression1.
$ typeset —i2 X $ ((X=2#10101 >> 2)); print "$X" 2#101 $ ((X=2#101 >> 1)); print "$X" 2#10 $ ((X>>=1)); print "$X" 2#1
Evaluates to 0 (true) if expression1 is less than or equal to expression2, otherwise evaluates to 1 (false).
$ ((1 <= 2)) && print "1 is less than 2" 1 is less than 2 $ ((3 <= 2)) || print "3 is not less than 2" 3 is not less than 2
Evaluates to 0 (true) if expression1 is less than expression2, otherwise evaluates to 1 (false).
$ ((1 < 2)); print "$?" 0 $ ((3 < 2)); print "$?" 1
Evaluates to 0 (true) if expression1 is greater than or equal to expression2, otherwise evaluates to 1 (false).
$ ((3 >= 2)) && print "3 is greater than 2" 3 is greater than 2 $ ((1 >= 2)) || print "1 is not greater than 2" 1 is not greater than 2
Evaluates to 0 (true) if expression1 is greater expression2, otherwise evaluates to 1 (false).
$ ((3 > 2)); print $? 0 $ ((1 > 2)); print $? 1
Evaluates to 0 (true) if expression1 is equal to expression2, otherwise evaluates to 1 (false).
$ ((3 == 3)) && print "3 is equal to 3" 3 is equal to 3 $ ((4 == 3)) || print "4 is not equal to 3" 4 is not equal to 3
Evaluates to 0 (true) if expression1 is not equal to expression2, otherwise evaluates to 1 (false).
$ ((4 != 3)); print "$?" 0 $ ((3 != 3 )); print "$?" 1
Returns a value that contains a 1 in each bit where there is a 1 in both expressions, and a 0 in every other bit. The second format assigned the result of the evaluation to expression1.
$ typeset –i2 X $ ((X=2#11 & 2#10)); print "$X" 2#10 $ ((X=2#101 & 2#111)); print "$X" 2#101 $ ((X&=2#001)); print "$X" 2#1
Returns a value that contains a 1 in each bit where there is a 1 in only one of the expressions, and a 0 in every other bit. The second format assigned the result of the evaluation to expression1.
$ typeset —i2 X $ ((X=2#11 ^ 2#10)); print "$X" 2#1 $ ((X=2#101 ^ 2#011)); print "$X" 2#110 $ ((X^=2#100)); print "$X" 2#10
Returns a value that contains a 1 in each bit where there is a 1 in either of the expressions, and a 0 in every other bit. The second format assigned the result of the evaluation to expression1.
$ typeset —i2 X $ ((X=2#11 | 2#10)); print "$X" 2#11 $ ((X=2#101 | 2#011)); print "$X" 2#111 $ ((X|=2#1001)); print "$X" 2#1111
If expression1 evaluates to 0 (true), expression2 is evaluated. The value of the entire expression is 0 (true) only if expression1 and expression2 are both true. Since both X and Y are equal to 1, the entire expression returns 0, and the print command is executed:
$ X=1 Y=1 $ ((X==1 && Y==1)) && print "X and Y equal 1" X and Y equal 1
Now only X is equal to 1, so the entire expression returns 1, and the print command is not executed:
$ unset Y $ ((X==1 && Y==1)) && print "X and Y equal 1" $
If expression1 evaluates to non-zero (false), expression2 is evaluated. The value of the entire expression is 0 (true) if either expression1 or expression2 are true. Since X is less than 2, the entire expression returns 0, and the print command is executed:
$ X=1 Y=2 $ ((X<2 || Y<5)) && print "X or Y less than 2" X or Y less than 2
Now, neither X nor Y are less than 2, so the entire expression returns 1, and the print command is not executed:
$ X=2 Y=2 $ ((X<2 || Y<5)) && print "X or Y less than 2" $
The () operators are used to override precedence rules. In the next expression, normal precedence rules cause the Y * Z operation to be performed first:
$ X=1 Y=2 Z=3 $ ((TMP=X + Y * Z)) $ print $TMP 7
If the expression is rewritten using the precedence override operators, the X + Y operation is performed first:
$ ((TMP=(X + Y) * Z)) $ print $TMP 9
3.144.91.24