]> git.ipfire.org Git - thirdparty/bash.git/blob - tests/builtins.tests
bash-5.2 distribution sources and documentation
[thirdparty/bash.git] / tests / builtins.tests
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 # tests for miscellaneous builtins not tested elsewhere
15 set +p
16 set +o posix
17
18 ulimit -S -c 0 2>/dev/null
19 ulimit -c -S -- 1000 2>/dev/null
20 ulimit -c 2>/dev/null
21
22 # check that break breaks loops
23 for i in a b c; do echo $i; break; echo bad-$i; done
24 echo end-1
25 for i in a b c; do echo $i; break 1; echo bad-$i; done
26 echo end-2
27 for i in a b c; do
28 for j in x y z; do
29 echo $i:$j
30 break
31 echo bad-$i
32 done
33 echo end-$i
34 done
35 echo end-3
36
37 # check that break breaks nested loops
38 for i in a b c; do
39 for j in x y z; do
40 echo $i:$j
41 break 2
42 echo bad-$i
43 done
44 echo end-$i
45 done
46 echo end
47
48 # check that continue continues loops
49 for i in a b c; do echo $i; continue; echo bad-$i ; done
50 echo end-1
51 for i in a b c; do echo $i; continue 1; echo bad-$i; done
52 echo end-2
53 for i in a b c; do
54 for j in x y z; do
55 echo $i:$j
56 continue
57 echo bad-$i-$j
58 done
59 echo end-$i
60 done
61 echo end-3
62
63 # check that continue breaks out of nested loops
64 for i in a b c; do
65 for j in x y z; do
66 echo $i:$j
67 continue 2
68 echo bad-$i-$j
69 done
70 echo end-$i
71 done
72 echo end
73
74 # check that `eval' re-evaluates arguments, but `builtin' and `command' do not
75 AVAR='$BVAR'
76 BVAR=foo
77
78 echo $AVAR
79 builtin echo $AVAR
80 command echo $AVAR
81 eval echo \$AVAR
82 eval echo $AVAR
83
84 # test out eval with a temp environment
85 AVAR=bar eval echo \$AVAR
86 BVAR=xxx eval echo $AVAR
87
88 unset -v AVAR BVAR
89
90 # test umask
91 mask=$(umask)
92 umask 022
93 umask
94 umask -S
95 umask -S u=rwx,g=rwx,o=rx >/dev/null # 002
96 umask
97 umask -S
98 umask -p
99 umask -p -S
100 umask 0
101 umask -S
102 umask ${mask} # restore original mask
103
104 # builtin/command without arguments should do nothing. maybe someday they will
105 builtin
106 command
107
108 # test enable
109 enable -ps
110
111 enable -aps ; enable -nps
112
113 enable -n test
114 case "$(type -t test)" in
115 builtin) echo oops -- enable -n test failed ;;
116 *) echo enable -n test worked ;;
117 esac
118
119 enable test
120 case "$(type -t test)" in
121 builtin) echo enable test worked ;;
122 *) echo oops -- enable test failed ;;
123 esac
124
125 # test options to exec
126 (exec -a specialname ${THIS_SH} -c 'echo $0' )
127 (exec -l -a specialname ${THIS_SH} -c 'echo $0' )
128 # test `clean' environment. if /bin/sh is bash, and the script version of
129 # printenv is run, there will be variables in the environment that bash
130 # sets on startup. Also test code that prefixes argv[0] with a dash.
131 (export FOO=BAR ; exec -c -l printenv ) | grep FOO
132 (FOO=BAR exec -c printenv ) | grep FOO
133
134 (export FOO=BAR ; exec printenv ) | grep FOO
135 (FOO=BAR exec printenv ) | grep FOO
136
137 # ok, forget everything about hashed commands
138 hash -r
139 hash
140
141 # this had better succeed, since command -p guarantees we will find the
142 # standard utilities
143 command -p hash rm
144
145 # check out source/.
146
147 # sourcing a zero-length-file had better not be an error
148 rm -f /tmp/zero-length-file
149 cp /dev/null /tmp/zero-length-file
150 . /tmp/zero-length-file
151 echo $?
152 rm /tmp/zero-length-file
153
154 AVAR=AVAR
155
156 . ./source1.sub
157 AVAR=foo . ./source1.sub
158
159 . ./source2.sub
160 echo $?
161
162 set -- a b c
163 . ./source3.sub
164
165 # make sure source with arguments does not change the shell's positional
166 # parameters, but that the sourced file sees the arguments as its
167 # positional parameters
168 echo "$@"
169 . ./source3.sub x y z
170 echo "$@"
171
172 # but if the sourced script sets the positional parameters explicitly, they
173 # should be reflected in the calling shell's positional parameters. this
174 # also tests one of the shopt options that controls source using $PATH to
175 # find the script
176 echo "$@"
177 shopt -u sourcepath
178 . source4.sub
179 echo "$@"
180
181 # this is complicated when the sourced scripts gets its own positional
182 # parameters from arguments to `.'
183 set -- a b c
184 echo "$@"
185 . source4.sub x y z
186 echo "$@"
187
188 # test out cd and $CDPATH
189 ${THIS_SH} ./builtins1.sub
190
191 # test behavior of `.' when given a non-existent file argument
192 ${THIS_SH} ./source5.sub
193
194 # test bugs in sourcing non-regular files, fixed post-bash-3.2
195 ${THIS_SH} ./source6.sub
196
197 # test bugs with source called from multiline aliases and other contexts
198 ${THIS_SH} ./source7.sub
199
200 # in posix mode, assignment statements preceding special builtins are
201 # reflected in the shell environment. `.' and `eval' need special-case
202 # code.
203 set -o posix
204 echo $AVAR
205 AVAR=foo . ./source1.sub
206 echo $AVAR
207
208 AVAR=AVAR
209 echo $AVAR
210 AVAR=foo eval echo \$AVAR
211 echo $AVAR
212
213 AVAR=AVAR
214 echo $AVAR
215 AVAR=foo :
216 echo $AVAR
217 set +o posix
218
219 # but assignment statements preceding `export' are always reflected in
220 # the environment
221 foo="" export foo
222 declare -p foo
223 unset foo
224
225 # assignment statements preceding `declare' should be displayed correctly,
226 # but not persist after the command
227 FOO='$$' declare -p FOO
228 declare -p FOO
229 unset FOO
230
231 # except for `declare -x', which should be equivalent to `export'
232 FOO='$$' declare -x FOO
233 declare -p FOO
234 unset FOO
235
236 # test out kill -l. bash versions prior to 2.01 did `kill -l num' wrong
237 sigone=$(kill -l | sed -n 's:^ 1) *\([^ ]*\)[ ].*$:\1:p')
238
239 case "$(kill -l 1)" in
240 ${sigone/SIG/}) echo ok;;
241 *) echo oops -- kill -l failure;;
242 esac
243
244 # kill -l and trap -l should display exactly the same output
245 sigonea=$(trap -l | sed -n 's:^ 1) *\([^ ]*\)[ ].*$:\1:p')
246
247 if [ "$sigone" != "$sigonea" ]; then
248 echo oops -- kill -l and trap -l differ
249 fi
250
251 # POSIX.2 says that exit statuses > 128 are mapped to signal names by
252 # subtracting 128 so you can find out what signal killed a process
253 case "$(kill -l $(( 128 + 1)) )" in
254 ${sigone/SIG/}) echo ok;;
255 *) echo oops -- kill -l 129 failure;;
256 esac
257
258 # out-of-range signal numbers should report the argument in the error
259 # message, not 128 less than the argument
260 kill -l 4096
261
262 # kill -l NAME should return the signal number
263 kill -l ${sigone/SIG/}
264
265 # test behavior of shopt xpg_echo
266 ${THIS_SH} ./builtins2.sub
267
268 # test behavior of declare -g
269 ${THIS_SH} ./builtins3.sub
270
271 # test behavior of using declare to create variables without assigning values
272 ${THIS_SH} ./builtins4.sub
273
274 # test behavior of set and unset array variables
275 ${THIS_SH} ./builtins5.sub
276
277 # test behavior of unset builtin with -f and -v options
278 ${THIS_SH} ./builtins6.sub
279
280 # test behavior of command builtin after changing it to a pseudo-keyword
281 ${THIS_SH} ./builtins7.sub
282
283 # this must be last -- it is a fatal error
284 exit status
285
286 echo after bad exit