1 #!/usr/bin/env bash
2 #
3 # NOTE: Could move spec/03-glob.sh here.
4
5 #### glob double quote escape
6 echo "*.sh"
7 ## stdout: *.sh
8
9 #### glob single quote escape
10 echo "*.sh"
11 ## stdout: *.sh
12
13 #### glob backslash escape
14 echo \*.sh
15 ## stdout: *.sh
16
17 #### 1 char glob
18 echo [b]in
19 ## stdout: bin
20
21 #### 0 char glob -- does NOT work
22 echo []bin
23 ## stdout: []bin
24
25 #### looks like glob at the start, but isn't
26 echo [bin
27 ## stdout: [bin
28
29 #### looks like glob plus negation at the start, but isn't
30 echo [!bin
31 ## stdout: [!bin
32
33 #### glob can expand to command and arg
34 spec/testdata/echo.s[hz]
35 ## stdout: spec/testdata/echo.sz
36
37 #### glob after var expansion
38 touch _tmp/a.A _tmp/aa.A _tmp/b.B
39 f="_tmp/*.A"
40 g="$f _tmp/*.B"
41 echo $g
42 ## stdout: _tmp/a.A _tmp/aa.A _tmp/b.B
43
44 #### quoted var expansion with glob meta characters
45 touch _tmp/a.A _tmp/aa.A _tmp/b.B
46 f="_tmp/*.A"
47 echo "[ $f ]"
48 ## stdout: [ _tmp/*.A ]
49
50 #### glob after "$@" expansion
51 fun() {
52 echo "$@"
53 }
54 fun '_tmp/*.B'
55 ## stdout: _tmp/*.B
56
57 #### glob after $@ expansion
58 fun() {
59 echo $@
60 }
61 fun '_tmp/*.B'
62 ## stdout: _tmp/b.B
63
64 #### no glob after ~ expansion
65 HOME=*
66 echo ~/*.py
67 ## stdout: */*.py
68
69 #### store literal globs in array then expand
70 touch _tmp/a.A _tmp/aa.A _tmp/b.B
71 g=("_tmp/*.A" "_tmp/*.B")
72 echo ${g[@]}
73 ## stdout: _tmp/a.A _tmp/aa.A _tmp/b.B
74 ## N-I dash/ash stdout-json: ""
75 ## N-I dash/ash status: 2
76
77 #### glob inside array
78 touch _tmp/a.A _tmp/aa.A _tmp/b.B
79 g=(_tmp/*.A _tmp/*.B)
80 echo "${g[@]}"
81 ## stdout: _tmp/a.A _tmp/aa.A _tmp/b.B
82 ## N-I dash/ash stdout-json: ""
83 ## N-I dash/ash status: 2
84
85 #### glob with escaped - in char class
86 touch _tmp/foo.-
87 touch _tmp/c.C
88 echo _tmp/*.[C-D] _tmp/*.[C\-D]
89 ## stdout: _tmp/c.C _tmp/c.C _tmp/foo.-
90
91 #### glob with char class expression
92 # note: mksh doesn't support [[:punct:]] ?
93 touch _tmp/e.E _tmp/foo.-
94 echo _tmp/*.[[:punct:]E]
95 ## stdout: _tmp/e.E _tmp/foo.-
96 ## BUG mksh stdout: _tmp/*.[[:punct:]E]
97
98 #### glob double quotes
99 # note: mksh doesn't support [[:punct:]] ?
100 touch _tmp/\"quoted.py\"
101 echo _tmp/\"*.py\"
102 ## stdout: _tmp/"quoted.py"
103
104 #### glob escaped
105 # - mksh doesn't support [[:punct:]] ?
106 # - python shell fails because \[ not supported!
107 touch _tmp/\[abc\] _tmp/\?
108 echo _tmp/\[???\] _tmp/\?
109 ## stdout: _tmp/[abc] _tmp/?
110
111 #### : escaped
112 touch _tmp/foo.-
113 echo _tmp/*.[[:punct:]] _tmp/*.[[:punct\:]]
114 ## stdout: _tmp/foo.- _tmp/*.[[:punct:]]
115 ## BUG mksh stdout: _tmp/*.[[:punct:]] _tmp/*.[[:punct:]]
116 ## BUG ash stdout: _tmp/foo.- _tmp/foo.-
117
118 #### Redirect to glob, not evaluated
119 # This writes to *.F, not foo.F
120 rm _tmp/*.F
121 touch _tmp/f.F
122 echo foo > _tmp/*.F
123 cat '_tmp/*.F'
124 ## status: 0
125 ## stdout: foo
126 ## BUG bash status: 1
127 ## BUG bash stdout-json: ""
128
129 #### Glob after var manipulation
130 touch _tmp/foo.zzz _tmp/bar.zzz
131 g='_tmp/*.zzzZ'
132 echo $g ${g%Z}
133 ## stdout: _tmp/*.zzzZ _tmp/bar.zzz _tmp/foo.zzz
134
135 #### Glob after part joining
136 touch _tmp/foo.yyy _tmp/bar.yyy
137 g='_tmp/*.yy'
138 echo $g ${g}y
139 ## stdout: _tmp/*.yy _tmp/bar.yyy _tmp/foo.yyy
140
141 #### Glob flags on file system
142 touch _tmp/-n _tmp/zzzzz
143 cd _tmp
144 echo -* hello zzzz?
145 ## stdout-json: "hello zzzzz"
146
147 #### set -o noglob
148 touch _tmp/spec-tmp/a.zz _tmp/spec-tmp/b.zz
149 echo _tmp/spec-tmp/*.zz
150 set -o noglob
151 echo _tmp/spec-tmp/*.zz
152 ## stdout-json: "_tmp/spec-tmp/a.zz _tmp/spec-tmp/b.zz\n_tmp/spec-tmp/*.zz\n"
153
154 #### shopt -s nullglob
155 argv.py _tmp/spec-tmp/*.nonexistent
156 shopt -s nullglob
157 argv.py _tmp/spec-tmp/*.nonexistent
158 ## stdout-json: "['_tmp/spec-tmp/*.nonexistent']\n[]\n"
159 ## N-I dash/mksh/ash stdout-json: "['_tmp/spec-tmp/*.nonexistent']\n['_tmp/spec-tmp/*.nonexistent']\n"
160
161 #### shopt -s failglob in command context
162 argv.py *.ZZ
163 shopt -s failglob
164 argv.py *.ZZ # nothing is printed, not []
165 echo status=$?
166 ## STDOUT:
167 ['*.ZZ']
168 status=1
169 ## END
170 ## N-I dash/mksh/ash STDOUT:
171 ['*.ZZ']
172 ['*.ZZ']
173 status=0
174 ## END
175
176 #### shopt -s failglob in loop context
177 for x in *.ZZ; do echo $x; done
178 echo status=$?
179 shopt -s failglob
180 for x in *.ZZ; do echo $x; done
181 echo status=$?
182 ## STDOUT:
183 *.ZZ
184 status=0
185 status=1
186 ## END
187 ## N-I dash/mksh/ash STDOUT:
188 *.ZZ
189 status=0
190 *.ZZ
191 status=0
192 ## END
193
194 #### Don't glob flags on file system with GLOBIGNORE
195 # This is a bash-specific extension.
196 expr $0 : '.*/osh$' >/dev/null && exit 99 # disabled until cd implemented
197 touch _tmp/-n _tmp/zzzzz
198 cd _tmp # this fail in osh
199 GLOBIGNORE=-*:zzzzz # colon-separated pattern list
200 echo -* hello zzzz?
201 ## stdout-json: "-* hello zzzz?\n"
202 ## N-I dash/mksh/ash stdout-json: "hello zzzzz"
203 ## status: 0
204
205 #### Splitting/Globbing doesn't happen on local assignment
206 f() {
207 # Dash splits words and globs before handing it to the 'local' builtin. But
208 # ash doesn't!
209 local foo=$1
210 echo "$foo"
211 }
212 f 'void *'
213 ## stdout: void *
214 ## BUG dash stdout-json: ""
215 ## BUG dash status: 2
216
217 #### Glob of unescaped [[] and []]
218 touch $TMP/[ $TMP/]
219 cd $TMP
220 echo [\[z] [\]z] # the right way to do it
221 echo [[z] []z] # also accepted
222 ## STDOUT:
223 [ ]
224 [ ]
225 ## END
226
227 #### Glob of negated unescaped [[] and []]
228 # osh does this "correctly" because it defers to libc!
229 touch $TMP/_G
230 cd $TMP
231 echo _[^\[z] _[^\]z] # the right way to do it
232 echo _[^[z] _[^]z] # also accepted
233 ## STDOUT:
234 _G _G
235 _G _G
236 ## END
237 ## BUG dash/mksh STDOUT:
238 _[^[z] _[^]z]
239 _[^[z] _[^]z]
240 ## END
241
242 #### PatSub of unescaped [[] and []]
243 x='[foo]'
244 echo ${x//[\[z]/<} # the right way to do it
245 echo ${x//[\]z]/>}
246 echo ${x//[[z]/<} # also accepted
247 echo ${x//[]z]/>}
248 ## STDOUT:
249 <foo]
250 [foo>
251 <foo]
252 [foo>
253 ## END
254 ## N-I dash stdout-json: ""
255 ## N-I dash status: 2
256
257 #### PatSub of negated unescaped [[] and []]
258 x='[foo]'
259 echo ${x//[^\[z]/<} # the right way to do it
260 echo ${x//[^\]z]/>}
261 echo ${x//[^[z]/<} # also accepted
262 #echo ${x//[^]z]/>} # only busybox ash interprets as ^\]
263 ## STDOUT:
264 [<<<<
265 >>>>]
266 [<<<<
267 ## END
268 # mksh is doing something very odd, ignoring ^ altogether?
269 ## BUG mksh STDOUT:
270 <foo]
271 [foo>
272 <foo]
273 ## END
274 ## N-I dash stdout-json: ""
275 ## N-I dash status: 2
276
277 #### Glob unicode char
278
279 touch $TMP/__a__
280 touch $TMP/__μ__
281 cd $TMP
282
283 echo __?__
284
285 ## STDOUT:
286 __a__ __μ__
287 ## END
288 ## BUG dash/mksh/ash STDOUT:
289 __a__
290 ## END
291 # note: zsh also passes this, but it doesn't run with this file.
292
293 #### dotglob (bash option that dashglob is roughly consistent with)
294 mkdir -p $TMP/dotglob
295 cd $TMP/dotglob
296 touch .foorc other
297
298 echo *
299 shopt -s dotglob
300 echo *
301 ## STDOUT:
302 other
303 .foorc other
304 ## END
305 ## N-I dash/mksh/ash STDOUT:
306 other
307 other
308 ## END
309