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
162 argv.py *.ZZ
163 shopt -s failglob
164 argv.py *.ZZ # nothing is printed, not []
165 echo status=$?
166 ## stdout-json: "['*.ZZ']\nstatus=1\n"
167 ## N-I dash/mksh/ash stdout-json: "['*.ZZ']\n['*.ZZ']\nstatus=0\n"
168
169 #### Don't glob flags on file system with GLOBIGNORE
170 # This is a bash-specific extension.
171 expr $0 : '.*/osh$' >/dev/null && exit 99 # disabled until cd implemented
172 touch _tmp/-n _tmp/zzzzz
173 cd _tmp # this fail in osh
174 GLOBIGNORE=-*:zzzzz # colon-separated pattern list
175 echo -* hello zzzz?
176 ## stdout-json: "-* hello zzzz?\n"
177 ## N-I dash/mksh/ash stdout-json: "hello zzzzz"
178 ## status: 0
179
180 #### Splitting/Globbing doesn't happen on local assignment
181 f() {
182 # Dash splits words and globs before handing it to the 'local' builtin. But
183 # ash doesn't!
184 local foo=$1
185 echo "$foo"
186 }
187 f 'void *'
188 ## stdout: void *
189 ## BUG dash stdout-json: ""
190 ## BUG dash status: 2
191
192 #### Glob of unescaped [[] and []]
193 touch $TMP/[ $TMP/]
194 cd $TMP
195 echo [\[z] [\]z] # the right way to do it
196 echo [[z] []z] # also accepted
197 ## STDOUT:
198 [ ]
199 [ ]
200 ## END
201
202 #### Glob of negated unescaped [[] and []]
203 # osh does this "correctly" because it defers to libc!
204 touch $TMP/_G
205 cd $TMP
206 echo _[^\[z] _[^\]z] # the right way to do it
207 echo _[^[z] _[^]z] # also accepted
208 ## STDOUT:
209 _G _G
210 _G _G
211 ## END
212 ## BUG dash/mksh STDOUT:
213 _[^[z] _[^]z]
214 _[^[z] _[^]z]
215 ## END
216
217 #### PatSub of unescaped [[] and []]
218 x='[foo]'
219 echo ${x//[\[z]/<} # the right way to do it
220 echo ${x//[\]z]/>}
221 echo ${x//[[z]/<} # also accepted
222 echo ${x//[]z]/>}
223 ## STDOUT:
224 <foo]
225 [foo>
226 <foo]
227 [foo>
228 ## END
229 ## N-I dash stdout-json: ""
230 ## N-I dash status: 2
231
232 #### PatSub of negated unescaped [[] and []]
233 x='[foo]'
234 echo ${x//[^\[z]/<} # the right way to do it
235 echo ${x//[^\]z]/>}
236 echo ${x//[^[z]/<} # also accepted
237 #echo ${x//[^]z]/>} # only busybox ash interprets as ^\]
238 ## STDOUT:
239 [<<<<
240 >>>>]
241 [<<<<
242 ## END
243 # mksh is doing something very odd, ignoring ^ altogether?
244 ## BUG mksh STDOUT:
245 <foo]
246 [foo>
247 <foo]
248 ## END
249 ## N-I dash stdout-json: ""
250 ## N-I dash status: 2
251
252 #### Glob unicode char
253
254 touch $TMP/__a__
255 touch $TMP/__μ__
256 cd $TMP
257
258 echo __?__
259
260 ## STDOUT:
261 __a__ __μ__
262 ## END
263 ## BUG dash/mksh/ash STDOUT:
264 __a__
265 ## END
266 # note: zsh also passes this, but it doesn't run with this file.