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