]> git.ipfire.org Git - thirdparty/bash.git/blob - tests/dollar-at-star
Bash-5.2-rc4 release
[thirdparty/bash.git] / tests / dollar-at-star
1 # This program is free software: you can redistribute it and/or modify
2 # it under the terms of the GNU General Public License as published by
3 # the Free Software Foundation, either version 3 of the License, or
4 # (at your option) any later version.
5 #
6 # This program is distributed in the hope that it will be useful,
7 # but WITHOUT ANY WARRANTY; without even the implied warranty of
8 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 # GNU General Public License for more details.
10 #
11 # You should have received a copy of the GNU General Public License
12 # along with this program. If not, see <http://www.gnu.org/licenses/>.
13 #
14
15 # first, let's start with the basics
16
17 recho "$@"
18 recho "$*"
19
20 recho $@
21 recho $*
22
23 foo=$*
24 foo=$@
25
26 foo="$*"
27 foo="$@"
28
29 unset -v bar
30
31 foo=${bar:-$*}
32 foo=${bar:-$@}
33
34 foo=${bar:-"$*"}
35 foo=${bar:-"$@"}
36
37 foo=${!*}
38 foo=${!@}
39
40 set a b
41
42 recho "$*"
43
44 # If IFS is null, the parameters are joined without separators
45 IFS=''
46 recho "$*"
47
48 # If IFS is unset, the parameters are separated by spaces
49 unset IFS
50 recho "${*}"
51
52 recho "$@"
53 recho $@
54
55 IFS='/'
56 set bob 'tom dick harry' joe
57 set $*
58 recho $#
59 recho $1
60 recho $2
61 recho $3
62
63 set bob 'tom dick harry' joe
64 set ${*}
65 recho $#
66 recho $1
67 recho $2
68 recho $3
69
70 set bob 'tom dick harry' joe
71 set $@
72 recho $#
73 recho $1
74 recho $2
75 recho $3
76
77 set bob 'tom dick harry' joe
78 set ${@}
79 recho $#
80 recho $1
81 recho $2
82 recho $3
83
84 # according to POSIX.2, unquoted $* should expand to multiple words if
85 # $IFS is null, just like unquoted $@
86 IFS=''
87 set bob 'tom dick harry' joe
88 set $*
89 recho $#
90 recho $1
91 recho $2
92 recho $3
93
94 set bob 'tom dick harry' joe
95 set $@
96 recho $#
97 recho $1
98 recho $2
99 recho $3
100
101 # if IFS is unset, the individual positional parameters are split on
102 # " \t\n" if $* or $@ are unquoted
103 unset IFS
104 set bob 'tom dick harry' joe
105 set $*
106 recho $#
107 recho $1
108 recho $2
109 recho $3
110
111 set bob 'tom dick harry' joe
112 set $@
113 recho $#
114 recho $1
115 recho $2
116 recho $3
117
118 # but not for "$@" or "$*"
119 set bob 'tom dick harry' joe
120 set "$*"
121 recho $#
122 recho $1
123 recho $2
124 recho $3
125
126 set bob 'tom dick harry' joe
127 set "$@"
128 recho $#
129 recho $1
130 recho $2
131 recho $3
132
133 # POSIX.2 says these should both expand the positional parameters
134 # to multiple words
135 set a b c d e
136 IFS=""
137 recho $@
138 recho "$@"
139
140 # this example is straight from the POSIX.2 rationale
141 set foo bar bam
142
143 recho "$@"
144 recho "$*"
145
146 unset IFS
147
148 recho "$@"
149 recho $@
150 recho "$*"
151
152 IFS=:
153
154 # special variables
155 set -- 1 2 3 4 5 6 7 8 9 10
156
157 bar=${*}
158 foo=$*
159 echo foo = "$foo"
160 echo bar = "$bar"
161
162 foo1=$@
163 bar1=${@}
164
165 echo foo1 = "$foo1"
166 echo bar1 = "$bar1"
167
168 foo2="$*"
169 bar2="${*}"
170
171 echo foo2 = "$foo2"
172 echo bar2 = "$bar2"
173
174 eval foo3='$*' bar3='${*}'
175 echo foo3 = "$foo3"
176 echo bar3 = "$bar3"
177
178 case $* in
179 *\:*) echo ok 1;;
180 *) echo bad 1;;
181 esac
182
183 case $@ in
184 *\:*) echo bad 2;;
185 *) echo ok 2;;
186 esac
187
188 case "$*" in
189 *\:*) echo ok 3;;
190 *) echo bad 3;;
191 esac
192
193 case "$@" in
194 *\:*) echo bad 4;;
195 *) echo ok 4;;
196 esac
197
198 IFS=$' \t\n'
199
200 bar=${*}
201 foo=$*
202 echo foo = "$foo"
203 echo bar = "$bar"
204
205 foo1=$@
206 bar1=${@}
207
208 echo foo1 = "$foo1"
209 echo bar1 = "$bar1"
210
211 foo2="$*"
212 bar2="${*}"
213
214 echo foo2 = "$foo2"
215 echo bar2 = "$bar2"
216
217 eval foo3='$*' bar3='${*}'
218 echo foo3 = "$foo3"
219 echo bar3 = "$bar3"
220
221 case $* in
222 *\ *) echo ok 1;;
223 *) echo bad 1;;
224 esac
225
226 case $@ in
227 *\ *) echo ok 2;;
228 *) echo bad 2;;
229 esac
230
231 case "$*" in
232 *\ *) echo ok 3;;
233 *) echo bad 3;;
234 esac
235
236 case "$@" in
237 *\ *) echo ok 4;;
238 *) echo bad 4;;
239 esac
240
241 # tests for the effect of quoting $* and $@ in an assignment context (plus
242 # arrays) -- bugs through bash 4.2
243 ${THIS_SH} ./dollar-at-star1.sub
244
245 # more tests for expanding $@ and $* in a context where there is no word
246 # splitting
247 ${THIS_SH} ./dollar-at-star2.sub
248 ${THIS_SH} ./dollar-at-star3.sub
249 ${THIS_SH} ./dollar-at-star4.sub
250 ${THIS_SH} ./dollar-at-star5.sub
251 ${THIS_SH} ./dollar-at-star6.sub
252 ${THIS_SH} ./dollar-at-star7.sub
253
254 # tests for expansions of $@ and ${a[@]} (vs. $* and ${a[*]}) on the RHS of
255 # assignment statements with non-default IFS: $@ expands to args or array
256 # members separated by spaces
257 ${THIS_SH} ./dollar-at-star8.sub
258
259 # more tests of the expansions of $@ and $* (and their array equivalents)
260 # with different values for IFS
261 ${THIS_SH} ./dollar-at-star9.sub
262
263 # tests for expansions of "$*" and "$@" and their array equivalents when $1 == ''
264 # and we're using the POSIX word expansions
265 ${THIS_SH} ./dollar-at-star10.sub
266 ${THIS_SH} ./dollar-at-star11.sub
267
268 # tests for special expansion of "$*" and "${array[*]}" when used with other
269 # expansions -- bugs through bash-2.05b
270 ${THIS_SH} ./dollar-star1.sub
271
272 # tests for expansion of "$@" on rhs of things like ${param:+word}. Bugs
273 # though bash-2.05b
274 ${THIS_SH} ./dollar-at1.sub
275
276 # tests for expansion of other variables in double-quoted strings containing
277 # $@. Bugs through bash-2.05b
278 ${THIS_SH} ./dollar-at2.sub
279
280 # tests for various expansions of $* in different contexts -- word split,
281 # no splitting, etc. when $IFS is NUL
282 ${THIS_SH} ./dollar-star2.sub
283
284 # tests for expansions of "${array[*]}" and "${array[@]}" when $IFS is not the
285 # default and the array contains null elements
286 ${THIS_SH} ./dollar-star3.sub
287
288 # test for set -u and expansions of $@ when there are no positional parameters
289 ${THIS_SH} ./dollar-at3.sub
290 # test for set -u and expansions of $* when there are no positional parameters
291 ${THIS_SH} ./dollar-star4.sub
292
293 # tests for expansions of $* when IFS is null
294 ${THIS_SH} ./dollar-star5.sub
295
296 # tests for inappropriate word splitting through bash-4.2
297 ${THIS_SH} ./dollar-at4.sub
298
299 # tests for problems with "$@" preceded and followed by other quoted expansions
300 # through bash-4.2
301 ${THIS_SH} ./dollar-at5.sub
302
303 # tests for problems with "${@:1}" and other expansions with null entries
304 # in positional parameters
305 ${THIS_SH} ./dollar-at6.sub
306
307 # tests for expansions of $* when $1 == ""; problem through bash-4.2
308 ${THIS_SH} ./dollar-star6.sub
309
310 # tests for expansions of $* (unquoted) when IFS changes (e.g., ${IFS:=-})
311 # problem through bash-4.2
312 ${THIS_SH} ./dollar-star7.sub
313
314 # tests for expansions of $* (unquoted) when IFS is null and word splitting is
315 # not going to be performed.
316 # problem through bash-4.4 in some parameter expansion contexts
317 ${THIS_SH} ./dollar-star8.sub
318
319 # tests for expansions of "$@" when there are no positional parameter or when
320 # $1 == '' and the expansion is preceded by something that results in a quoted
321 # null string
322 ${THIS_SH} ./dollar-at7.sub
323
324 # tests for expansions of $* when in an assignment context (no splitting) and
325 # IFS is null
326 ${THIS_SH} ./dollar-star9.sub
327
328 # more tests for expansions of $* when not splitting with IFS set or unset and
329 # null strings as the positional parameters
330 ${THIS_SH} ./dollar-star10.sub
331
332 exit 0