]> git.ipfire.org Git - thirdparty/bash.git/blame - tests/errors.tests
bash-5.2 distribution sources and documentation
[thirdparty/bash.git] / tests / errors.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# These should all be safe
15LC_ALL=C
16LC_CTYPE=C
17LC_COLLATE=C
18LC_MESSAGES=C
19
20# these tests should all generate errors
21
22# make sure we don't exit prematurely
23set +e
24set +o posix
25
cce855bc
JA
26# various alias/unalias errors
27
28# at some point, this may mean to `export' an alias, like ksh, but
29# for now it is an error
30alias -x foo=barz
31unalias -x fooaha
32alias hoowah
33unalias hoowah
34
d166f048
JA
35# the iteration variable must be a valid identifier
36for 1 in a b c; do echo $1; done
37
38# try to rebind a read-only function
39func()
40{
41 echo func
42}
43readonly -f func
44# make sure `readonly' and `declare' play well together
45declare -Fr
46func()
47{
48 echo bar
49}
50
cce855bc
JA
51# bad option
52unset -x func
53
d166f048
JA
54# cannot unset readonly functions or variables
55unset -f func
56# or make them not readonly
57declare -fr func
58declare -f +r func
59
74091dd4
CR
60# cannot use declare -f in combination with other attributes
61a() { echo a; }
62declare -f -a a
63declare -f -i b c
64
d166f048
JA
65XPATH=$PATH
66declare -r XPATH
67unset -v XPATH
68
69# cannot unset invalid identifiers
70unset /bin/sh
71
cce855bc
JA
72# cannot unset function and variable at the same time
73unset -f -v SHELL
74
d166f048
JA
75# bad option
76declare -z
77# cannot declare invalid identifiers
78declare -- -z
79declare /bin/sh
80
81# this is the syntax used to export functions in the environment, but
82# it cannot be used with `declare'
83declare -f func='() { echo "this is func"; }'
84
cce855bc
JA
85# bad option to exec -- this should not exit the script
86exec -i /bin/sh
87
d166f048
JA
88# try to export -f something that is not a function -- this should be
89# an error, not create an `invisible function'
90export -f XPATH
91
92# this depends on the setting of BREAK_COMPLAINS in config.h.in
93break
94continue
95
96# this should not exit the shell; it did in versions before 2.01
97shift label
98
99# other shells do not complain about the extra arguments; maybe someday
100# we won't either
101set -- a b c
102shift $# label
103# and get rid of the positional parameters
104shift $#
105
106# let without an expression is an error, though maybe it should just return
107# success
108let
109
110# local outside a function is an error
111local
112
cce855bc
JA
113# logout of a non-login shell is an error
114logout
115
8868edaf 116# try to hash a non-existent command
d166f048
JA
117hash notthere
118
cce855bc
JA
119# bad option to hash, although it may mean `verbose' at some future point
120hash -v
121
d166f048
JA
122# turn off hashing, then try to hash something
123set +o hashall
124hash -p ${THIS_SH} ${THIS_SH##*/}
125
126# bad identifiers to declare/readonly/export
127export AA[4]
128readonly AA[4]
129
130declare -a AA
131unset AA[-2]
132
133# try to assign to a readonly array
134declare -r AA
135AA=( one two three )
136
cce855bc
JA
137# make sure `readonly -n' doesn't turn off readonly status
138readonly -n AA
139AA=(one two three)
140
141# try to assign a readonly array with bad assignment syntax
f73dda09
JA
142# NOTE: this works in post-bash-2.05 (at least when I write this)
143# readonly -a ZZZ=bbb
cce855bc 144
d166f048
JA
145# bad counts to `shift'
146shopt -s shift_verbose
147shift $(( $# + 5 ))
148shift -2
149
150# bad shell options
151shopt -s no_such_option
152shopt no_such_option
153
154# non-octal digits for umask and other errors
155umask 09
156umask -S u=rwx:g=rwx:o=rx >/dev/null # 002
157umask -S u:rwx,g:rwx,o:rx >/dev/null # 002
cce855bc
JA
158
159# at some point, this may mean `invert', but for now it is an error
160umask -i
d166f048 161
bb70624e
JA
162# bad assignments shouldn't change the umask
163mask=$(umask)
164umask g=u
165mask2=$(umask)
166if [ "$mask" != "$mask2" ]; then
167 echo "umask errors change process umask"
168fi
169
d166f048
JA
170# assignment to a readonly variable in environment
171VAR=4
172readonly VAR
173VAR=7 :
174
175# more readonly variable tests
176declare VAR=88
177declare +r VAR
178
179declare -p unset
180
181# iteration variable in a for statement being readonly
182for VAR in 1 2 3 ; do echo $VAR; done
183
74091dd4
CR
184# parser errors; caught early so we have to run them in subshells
185${THIS_SH} -c ': $( for z in 1 2 3; do )' comsub
186${THIS_SH} -c ': $( for z in 1 2 3; done )' comsub
d166f048
JA
187
188# various `cd' errors
189( unset HOME ; cd )
cce855bc
JA
190( HOME=/tmp/xyz.bash ; cd )
191# errors from cd
192cd -
193cd /bin/sh # error - not a directory
194OLDPWD=/tmp/cd-notthere
195cd -
d166f048
JA
196
197# various `source/.' errors
198.
199source
200
201# maybe someday this will work like in rc
202. -i /dev/tty
203
204# make sure that this gives an error rather than setting $1
205set -q
206
207# enable non-builtins
208enable sh bash
209
210# try to set and unset shell options simultaneously
211shopt -s -u checkhash
212
bb70624e 213# this is an error -- bad timeout spec
cce855bc
JA
214read -t var < /dev/null
215
d166f048
JA
216# try to read into an invalid identifier
217read /bin/sh < /dev/null
218
219# try to read into a readonly variable
220read VAR < /dev/null
221
cce855bc
JA
222# bad option to readonly/export
223readonly -x foo
224
d166f048
JA
225# someday these may mean something, but for now they're errors
226eval -i "echo $-"
227command -i "echo $-"
228
cce855bc
JA
229# this caused a core dump in bash-2.01 (fixed in bash-2.01.1)
230eval echo \$[/bin/sh + 0]
231eval echo '$((/bin/sh + 0))'
232
d166f048
JA
233# error to list trap for an unknown signal
234trap -p NOSIG
235
236# maybe someday trap will take a -s argument like kill, but not now
237trap -p -s NOSIG
238
f73dda09
JA
239# we have a ksh-like ERR trap, post-bash-2.05
240#trap 'echo [$LINENO] -- error' ERR
d166f048
JA
241
242# can only return from a function or sourced script
243return 2
244
245# break and continue with arguments <= 0
246for z in 1 2 3; do
247 break 0
248 echo $x
249done
250for z in 1 2 3; do
251 continue 0
252 echo $x
253done
254
255# builtin with non-builtin
256builtin bash
257
258# maybe someday you will be able to use fg/bg when job control is not really
259# active, but for now they are errors
260bg
261fg
262
263# argument required
264kill -s
265# bad argument
266kill -S
cce855bc
JA
267# null argument
268kill -INT ''
269# argument required
270kill -INT
271
272# bad shell option names
273set -o trackall # bash is not ksh
d166f048 274
ac50fbac
CR
275# problem with versions through bash-4.2
276readonly xx=5
277echo $((xx=5))
278echo $?
279
280${THIS_SH} ./errors1.sub
281${THIS_SH} ./errors2.sub
282${THIS_SH} ./errors3.sub
a0c0a00f
CR
283${THIS_SH} ./errors4.sub
284${THIS_SH} -o posix ./errors4.sub
285
286${THIS_SH} ./errors5.sub
287
288${THIS_SH} ./errors6.sub
289THIS_SH="${THIS_SH} -o posix" ${THIS_SH} ./errors6.sub
ac50fbac 290
d233b485
CR
291${THIS_SH} ./errors7.sub
292${THIS_SH} -o posix ./errors7.sub
293
294${THIS_SH} ./errors8.sub
74091dd4 295${THIS_SH} ./errors9.sub
d233b485 296
8868edaf
CR
297${THIS_SH} -c 'return ; echo after return' bash
298${THIS_SH} -o posix -c 'return ; echo after return' bash
299
d166f048
JA
300# this must be last!
301# in posix mode, a function name must be a valid identifier
302# this can't go in posix2.tests, since it causes the shell to exit
303# immediately
304set -o posix
305function !! () { fc -s "$@" ; }
306set +o posix
307
308echo end