-
-
Notifications
You must be signed in to change notification settings - Fork 151
/
word-split.test.sh
421 lines (351 loc) · 6.54 KB
/
word-split.test.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
## compare_shells: bash dash mksh
## oils_failures_allowed: 8
# NOTE on bash bug: After setting IFS to array, it never splits anymore? Even
# if you assign IFS again.
#### IFS is scoped
IFS=b
word=abcd
f() { local IFS=c; argv.py $word; }
f
argv.py $word
## stdout-json: "['ab', 'd']\n['a', 'cd']\n"
#### Tilde sub is not split, but var sub is
HOME="foo bar"
argv.py ~
argv.py $HOME
## stdout-json: "['foo bar']\n['foo', 'bar']\n"
#### Word splitting
a="1 2"
b="3 4"
argv.py $a"$b"
## stdout-json: "['1', '23 4']\n"
#### Word splitting 2
a="1 2"
b="3 4"
c="5 6"
d="7 8"
argv.py $a"$b"$c"$d"
## stdout-json: "['1', '23 45', '67 8']\n"
# Has tests on differences between $* "$*" $@ "$@"
# http://stackoverflow.com/questions/448407/bash-script-to-receive-and-repass-quoted-parameters
#### $*
fun() { argv.py -$*-; }
fun "a 1" "b 2" "c 3"
## stdout: ['-a', '1', 'b', '2', 'c', '3-']
#### "$*"
fun() { argv.py "-$*-"; }
fun "a 1" "b 2" "c 3"
## stdout: ['-a 1 b 2 c 3-']
#### $@
# How does this differ from $* ? I don't think it does.
fun() { argv.py -$@-; }
fun "a 1" "b 2" "c 3"
## stdout: ['-a', '1', 'b', '2', 'c', '3-']
#### "$@"
fun() { argv.py "-$@-"; }
fun "a 1" "b 2" "c 3"
## stdout: ['-a 1', 'b 2', 'c 3-']
#### empty argv
argv.py 1 "$@" 2 $@ 3 "$*" 4 $* 5
## stdout: ['1', '2', '3', '', '4', '5']
#### Word elision with space
s1=' '
argv.py $s1
## stdout: []
#### Word elision with non-whitespace IFS
# Treated differently than the default IFS. What is the rule here?
IFS='_'
char='_'
space=' '
empty=''
argv.py $char
argv.py $space
argv.py $empty
## STDOUT:
['']
[' ']
[]
## END
#### Leading/trailing word elision with non-whitespace IFS
# This behavior is weird.
IFS=_
s1='_a_b_'
argv.py $s1
## stdout: ['', 'a', 'b']
#### Leading ' ' vs leading ' _ '
# This behavior is weird, but all shells agree.
IFS='_ '
s1='_ a b _ '
s2=' a b _ '
argv.py $s1
argv.py $s2
## STDOUT:
['', 'a', 'b']
['a', 'b']
## END
#### Multiple non-whitespace IFS chars.
IFS=_-
s1='a__b---c_d'
argv.py $s1
## stdout: ['a', '', 'b', '', '', 'c', 'd']
#### IFS with whitespace and non-whitepace.
# NOTE: Three delimiters means two empty words in the middle. No elision.
IFS='_ '
s1='a_b _ _ _ c _d e'
argv.py $s1
## stdout: ['a', 'b', '', '', 'c', 'd', 'e']
#### empty $@ and $* is elided
fun() { argv.py 1 $@ $* 2; }
fun
## stdout: ['1', '2']
#### unquoted empty arg is elided
empty=""
argv.py 1 $empty 2
## stdout: ['1', '2']
#### unquoted whitespace arg is elided
space=" "
argv.py 1 $space 2
## stdout: ['1', '2']
#### empty literals are not elided
space=" "
argv.py 1 $space"" 2
## stdout: ['1', '', '2']
#### no splitting when IFS is empty
IFS=""
foo="a b"
argv.py $foo
## stdout: ['a b']
#### default value can yield multiple words
argv.py 1 ${undefined:-"2 3" "4 5"} 6
## stdout: ['1', '2 3', '4 5', '6']
#### default value can yield multiple words with part joining
argv.py 1${undefined:-"2 3" "4 5"}6
## stdout: ['12 3', '4 56']
#### default value with unquoted IFS char
IFS=_
argv.py 1${undefined:-"2_3"x_x"4_5"}6
## stdout: ['12_3x', 'x4_56']
#### IFS empty doesn't do splitting
IFS=''
x=$(echo -e ' a b\tc\n')
argv.py $x
## STDOUT:
[' a b\tc']
## END
## N-I dash STDOUT:
['-e a b\tc']
## END
#### IFS unset behaves like $' \t\n'
unset IFS
x=$(echo -e ' a b\tc\n')
argv.py $x
## STDOUT:
['a', 'b', 'c']
## END
## N-I dash STDOUT:
['-e', 'a', 'b', 'c']
## END
#### IFS='\'
# NOTE: OSH fails this because of double backslash escaping issue!
IFS='\'
s='a\b'
argv.py $s
## STDOUT:
['a', 'b']
## END
#### IFS='\ '
# NOTE: OSH fails this because of double backslash escaping issue!
# When IFS is \, then you're no longer using backslash escaping.
IFS='\ '
s='a\b \\ c d\'
argv.py $s
## STDOUT:
['a', 'b', '', 'c', 'd']
## END
#### IFS characters are glob metacharacters
IFS='* '
s='a*b c'
argv.py $s
IFS='?'
s='?x?y?z?'
argv.py $s
IFS='['
s='[x[y[z['
argv.py $s
## STDOUT:
['a', 'b', 'c']
['', 'x', 'y', 'z']
['', 'x', 'y', 'z']
## END
#### Trailing space
argv.py 'Xec ho '
argv.py X'ec ho '
argv.py X"ec ho "
## STDOUT:
['Xec ho ']
['Xec ho ']
['Xec ho ']
## END
#### Empty IFS (regression for bug)
IFS=
echo ["$*"]
set a b c
echo ["$*"]
## STDOUT:
[]
[abc]
## END
#### Unset IFS (regression for bug)
set a b c
unset IFS
echo ["$*"]
## STDOUT:
[a b c]
## END
#### IFS=o (regression for bug)
IFS=o
echo hi
## STDOUT:
hi
## END
#### IFS and joining arrays
IFS=:
set -- x 'y z'
argv.py "$@"
argv.py $@
argv.py "$*"
argv.py $*
## STDOUT:
['x', 'y z']
['x', 'y z']
['x:y z']
['x', 'y z']
## END
#### IFS and joining arrays by assignments
IFS=:
set -- x 'y z'
s="$@"
argv.py "$s"
s=$@
argv.py "$s"
s"$*"
argv.py "$s"
s=$*
argv.py "$s"
# bash and mksh agree, but this doesn't really make sense to me.
# In OSH, "$@" is the only real array, so that's why it behaves differently.
## STDOUT:
['x y z']
['x y z']
['x y z']
['x:y z']
## END
## OK dash STDOUT:
['x:y z']
['x:y z']
['x:y z']
['x:y z']
## END
# TODO:
# - unquoted args of whitespace are not elided (when IFS = null)
# - empty quoted args are kept
#
# - $* $@ with empty IFS
# - $* $@ with custom IFS
#
# - no splitting when IFS is empty
# - word splitting removes leading and trailing whitespace
# TODO: test framework needs common setup
# Test IFS and $@ $* on all these
#### TODO
empty=""
space=" "
AB="A B"
X="X"
Yspaces=" Y "
#### IFS='' with $@ and $* (bug #627)
set -- a 'b c'
IFS=''
argv.py at $@
argv.py star $*
# zsh agrees
## STDOUT:
['at', 'a', 'b c']
['star', 'a', 'b c']
## END
## BUG ash STDOUT:
['at', 'ab c']
['star', 'ab c']
## END
#### IFS='' with $@ and $* and printf (bug #627)
set -- a 'b c'
IFS=''
printf '[%s]\n' $@
printf '[%s]\n' $*
## STDOUT:
[a]
[b c]
[a]
[b c]
## END
## BUG ash STDOUT:
[ab c]
[ab c]
## END
#### IFS='' with ${a[@]} and ${a[*]} (bug #627)
myarray=(a 'b c')
IFS=''
argv.py at ${myarray[@]}
argv.py star ${myarray[*]}
## STDOUT:
['at', 'a', 'b c']
['star', 'a', 'b c']
## END
## N-I dash/ash status: 2
## N-I dash/ash stdout-json: ""
#### Bug #628 split on : with : in literal word
IFS=':'
word='a:'
argv.py ${word}:b
argv.py ${word}:
echo ---
# Same thing happens for 'z'
IFS='z'
word='az'
argv.py ${word}zb
argv.py ${word}z
## STDOUT:
['a', ':b']
['a', ':']
---
['a', 'zb']
['a', 'z']
## END
#### Bug #698, similar crash
var='\'
set -f
echo $var
## STDOUT:
\
## END
#### Bug #1664, \\ with noglob
# Note that we're not changing IFS
argv.py [\\]_
argv.py "[\\]_"
# TODO: no difference observed here, go back to original bug
#argv.py [\\_
#argv.py "[\\_"
echo noglob
# repeat cases with -f, noglob
set -f
argv.py [\\]_
argv.py "[\\]_"
#argv.py [\\_
#argv.py "[\\_"
## STDOUT:
['[\\]_']
['[\\]_']
noglob
['[\\]_']
['[\\]_']
## END