OILS / spec / var-op-test.test.sh View on Github | oilshell.org

339 lines, 197 significant
1
2#### Lazy Evaluation of Alternative
3i=0
4x=x
5echo ${x:-$((i++))}
6echo $i
7echo ${undefined:-$((i++))}
8echo $i # i is one because the alternative was only evaluated once
9## status: 0
10## stdout-json: "x\n0\n0\n1\n"
11## N-I dash status: 2
12## N-I dash stdout-json: "x\n0\n"
13
14#### Default value when empty
15empty=''
16echo ${empty:-is empty}
17## stdout: is empty
18
19#### Default value when unset
20echo ${unset-is unset}
21## stdout: is unset
22
23#### Unquoted with array as default value
24set -- '1 2' '3 4'
25argv.py X${unset=x"$@"x}X
26argv.py X${unset=x$@x}X # If you want OSH to split, write this
27# osh
28## STDOUT:
29['Xx1', '2', '3', '4xX']
30['Xx1', '2', '3', '4xX']
31## END
32## OK osh STDOUT:
33['Xx1 2', '3 4xX']
34['Xx1', '2', '3', '4xX']
35## END
36
37#### Quoted with array as default value
38set -- '1 2' '3 4'
39argv.py "X${unset=x"$@"x}X"
40argv.py "X${unset=x$@x}X" # OSH is the same here
41## STDOUT:
42['Xx1 2 3 4xX']
43['Xx1 2 3 4xX']
44## END
45## BUG bash STDOUT:
46['Xx1', '2', '3', '4xX']
47['Xx1 2 3 4xX']
48## END
49## OK osh STDOUT:
50['Xx1 2', '3 4xX']
51['Xx1 2 3 4xX']
52## END
53
54#### Assign default with array
55set -- '1 2' '3 4'
56argv.py X${unset=x"$@"x}X
57argv.py "$unset"
58## STDOUT:
59['Xx1', '2', '3', '4xX']
60['x1 2 3 4x']
61## END
62## OK osh STDOUT:
63['Xx1 2', '3 4xX']
64['x1 2 3 4x']
65## END
66
67#### Assign default value when empty
68empty=''
69${empty:=is empty}
70echo $empty
71## stdout: is empty
72
73#### Assign default value when unset
74${unset=is unset}
75echo $unset
76## stdout: is unset
77
78#### ${v:+foo} Alternative value when empty
79v=foo
80empty=''
81echo ${v:+v is not empty} ${empty:+is not empty}
82## stdout: v is not empty
83
84#### ${v+foo} Alternative value when unset
85v=foo
86echo ${v+v is not unset} ${unset:+is not unset}
87## stdout: v is not unset
88
89#### "${x+foo}" quoted (regression)
90# Python's configure caught this
91argv.py "${with_icc+set}" = set
92## STDOUT:
93['', '=', 'set']
94## END
95
96#### ${v+foo} and ${v:+foo} when set -u
97set -u
98v=v
99echo v=${v:+foo}
100echo v=${v+foo}
101unset v
102echo v=${v:+foo}
103echo v=${v+foo}
104## STDOUT:
105v=foo
106v=foo
107v=
108v=
109## END
110
111#### ${v-foo} and ${v:-foo} when set -u
112set -u
113v=v
114echo v=${v:-foo}
115echo v=${v-foo}
116unset v
117echo v=${v:-foo}
118echo v=${v-foo}
119## STDOUT:
120v=v
121v=v
122v=foo
123v=foo
124## END
125
126#### array and - and +
127case $SH in (dash) exit ;; esac
128
129shopt -s compat_array # to refer to array as scalar
130
131empty=()
132a1=('')
133a2=('' x)
134a3=(3 4)
135echo empty=${empty[@]-minus}
136echo a1=${a1[@]-minus}
137echo a1[0]=${a1[0]-minus}
138echo a2=${a2[@]-minus}
139echo a3=${a3[@]-minus}
140echo ---
141
142echo empty=${empty[@]+plus}
143echo a1=${a1[@]+plus}
144echo a1[0]=${a1[0]+plus}
145echo a2=${a2[@]+plus}
146echo a3=${a3[@]+plus}
147echo ---
148
149echo empty=${empty+plus}
150echo a1=${a1+plus}
151echo a2=${a2+plus}
152echo a3=${a3+plus}
153echo ---
154
155# Test quoted arrays too
156argv.py "${empty[@]-minus}"
157argv.py "${empty[@]+plus}"
158argv.py "${a1[@]-minus}"
159argv.py "${a1[@]+plus}"
160argv.py "${a1[0]-minus}"
161argv.py "${a1[0]+plus}"
162argv.py "${a2[@]-minus}"
163argv.py "${a2[@]+plus}"
164argv.py "${a3[@]-minus}"
165argv.py "${a3[@]+plus}"
166
167## STDOUT:
168empty=minus
169a1=
170a1[0]=
171a2= x
172a3=3 4
173---
174empty=
175a1=plus
176a1[0]=plus
177a2=plus
178a3=plus
179---
180empty=
181a1=plus
182a2=plus
183a3=plus
184---
185['minus']
186[]
187['']
188['plus']
189['']
190['plus']
191['', 'x']
192['plus']
193['3', '4']
194['plus']
195## END
196## N-I dash stdout-json: ""
197
198#### $@ and - and +
199echo argv=${@-minus}
200echo argv=${@+plus}
201echo argv=${@:-minus}
202echo argv=${@:+plus}
203## STDOUT:
204argv=minus
205argv=
206argv=minus
207argv=
208## END
209## BUG dash STDOUT:
210argv=
211argv=plus
212argv=minus
213argv=
214## END
215
216#### assoc array and - and +
217case $SH in (dash|mksh) exit ;; esac
218
219declare -A empty=()
220declare -A assoc=(['k']=v)
221
222echo empty=${empty[@]-minus}
223echo empty=${empty[@]+plus}
224echo assoc=${assoc[@]-minus}
225echo assoc=${assoc[@]+plus}
226
227echo ---
228echo empty=${empty[@]:-minus}
229echo empty=${empty[@]:+plus}
230echo assoc=${assoc[@]:-minus}
231echo assoc=${assoc[@]:+plus}
232## STDOUT:
233empty=minus
234empty=
235assoc=v
236assoc=plus
237---
238empty=minus
239empty=
240assoc=v
241assoc=plus
242## END
243## N-I dash/mksh stdout-json: ""
244
245
246#### Error when empty
247empty=''
248echo ${empty:?'is em'pty} # test eval of error
249echo should not get here
250## stdout-json: ""
251## status: 1
252## OK dash status: 2
253
254#### Error when unset
255echo ${unset?is empty}
256echo should not get here
257## stdout-json: ""
258## status: 1
259## OK dash status: 2
260
261#### Error when unset
262v=foo
263echo ${v+v is not unset} ${unset:+is not unset}
264## stdout: v is not unset
265
266#### ${var=x} dynamic scope
267f() { : "${hello:=x}"; echo $hello; }
268f
269echo hello=$hello
270
271f() { hello=x; }
272f
273echo hello=$hello
274## STDOUT:
275x
276hello=x
277hello=x
278## END
279
280#### array ${arr[0]=x}
281arr=()
282echo ${#arr[@]}
283: ${arr[0]=x}
284echo ${#arr[@]}
285## STDOUT:
2860
2871
288## END
289## N-I dash status: 2
290## N-I dash stdout-json: ""
291
292#### assoc array ${arr["k"]=x}
293# note: this also works in zsh
294
295declare -A arr=()
296echo ${#arr[@]}
297: ${arr['k']=x}
298echo ${#arr[@]}
299## STDOUT:
3000
3011
302## END
303## N-I dash status: 2
304## N-I dash stdout-json: ""
305## N-I mksh status: 1
306## N-I mksh stdout-json: ""
307
308#### "\z" as arg
309echo "${undef-\$}"
310echo "${undef-\(}"
311echo "${undef-\z}"
312echo "${undef-\"}"
313echo "${undef-\`}"
314echo "${undef-\\}"
315## STDOUT:
316$
317\(
318\z
319"
320`
321\
322## END
323## BUG yash STDOUT:
324$
325(
326z
327"
328`
329\
330## END
331
332#### "\e" as arg
333echo "${undef-\e}"
334## STDOUT:
335\e
336## END
337## BUG zsh/mksh stdout-repr: '\x1b\n'
338## BUG yash stdout: e
339