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