1 #### Minus operator is left associative
2
3 var a = 1 - 0 - 1
4 var b = (1 - 0) - 1
5 echo a=$a b=$b
6
7 var a = 3 - 1 - 2
8 var b = (3 - 1) - 2
9 echo a=$a b=$b
10
11 ## STDOUT:
12 a=0 b=0
13 a=0 b=0
14 ## END
15
16 #### Division operators are left associative
17
18 var a = 10 / 4 / 2
19 var b = 10 / 4 / 2
20 echo a=$a b=$b
21
22 var a = 9 // 3 // 3
23 var b = (9 // 3) // 3
24 echo a=$a b=$b
25
26 var a = 11 % 6 % 3
27 var b = (11 % 6) % 3
28 echo a=$a b=$b
29
30 ## STDOUT:
31 a=1.25 b=1.25
32 a=1 b=1
33 a=2 b=2
34 ## END
35
36 #### Exponentiation is right associative
37
38 var a = 3 ** 2 ** 2
39 var b = 3 ** (2 ** 2)
40 echo a=$a b=$b
41
42 ## STDOUT:
43 a=81 b=81
44 ## END
45
46 #### Binary operators, with conversions from string
47
48 echo ' i i' $[1 + 2]
49 echo 'si i' $['1' + 2]
50 echo ' i si' $[1 + '2']
51 echo ---
52
53 echo ' f f' $[2.5 - 1.5]
54 echo 'sf f' $['2.5' - 1.5]
55 echo ' f sf' $[2.5 - '1.5']
56 echo ---
57
58 echo ' i f' $[4 * 1.5]
59 echo 'si f' $['4' * 1.5]
60 echo ' i sf' $[4 * '1.5']
61 echo ---
62
63 echo ' f i' $[5.0 / 2]
64 echo 'sf i' $['5.0' / 2]
65 echo ' f si' $[5.0 / '2']
66
67 ## STDOUT:
68 i i 3
69 si i 3
70 i si 3
71 ---
72 f f 1.0
73 sf f 1.0
74 f sf 1.0
75 ---
76 i f 6.0
77 si f 6.0
78 i sf 6.0
79 ---
80 f i 2.5
81 sf i 2.5
82 f si 2.5
83 ## END
84
85 #### Floating Point Division with /
86
87 var i = '1.0' / '0.05'
88
89 echo $i
90
91 ## STDOUT:
92 20.0
93 ## END
94
95
96 #### Operations That Convert to Integer: // % **
97 shopt -s parse_brace
98
99 var m = ' 5 ' // 2
100
101 var n = ' 5 ' % 2
102
103 var p = ' 5 ' ** 2
104
105 write -- $m $n $p
106
107 try {
108 var z = 'a' // 3
109 }
110 echo _status $_status
111
112 try {
113 var z = 'z' % 3
114 }
115 echo _status $_status
116
117 ## STDOUT:
118 2
119 1
120 25
121 _status 3
122 _status 3
123 ## END
124
125 #### Division by zero
126 shopt -s parse_brace
127
128 try {
129 = 42 / 0
130 }
131 echo "status / is $_status"
132
133 try {
134 = 42 // 0
135 }
136 echo "status // is $_status"
137
138 try {
139 = 42 % 0
140 }
141 echo "status % is $_status"
142
143 ## STDOUT:
144 status / is 3
145 status // is 3
146 status % is 3
147 ## END
148
149 #### Unary Operations
150
151 var a = ~1
152
153 var b = -1
154 var c = -2.3
155
156 var d = not true
157
158
159 write -- $a $b $c $d
160
161 ## STDOUT:
162 -2
163 -1
164 -2.3
165 false
166 ## END
167
168
169 #### unary minus on strings
170 json write (-3)
171 json write (-'4')
172 json write (-'5.5')
173
174 # Not accepted
175 json write (-'abc')
176
177 ## status: 3
178 ## STDOUT:
179 -3
180 -4
181 -5.5
182 ## END
183
184 #### unary ~ complement on strings
185 json write (~0)
186 json write (~'1')
187 json write (~' 2 ')
188 # Not accepted
189 json write (~'3.5')
190
191 ## status: 3
192 ## STDOUT:
193 -1
194 -2
195 -3
196 ## END
197
198 #### unary ~ doesn't work on bool
199 = ~false
200 ## status: 3
201 ## STDOUT:
202 ## END
203
204 #### unary ~ doesn't work on float
205 = ~1.0
206 ## status: 3
207 ## STDOUT:
208 ## END
209
210 #### unary - applied to bool is not allowed
211 = ~false
212 ## status: 3
213 ## STDOUT:
214 ## END