1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
|
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# These should all be safe
LC_ALL=C
LC_CTYPE=C
LC_COLLATE=C
LC_MESSAGES=C
# these tests should all generate errors
# make sure we don't exit prematurely
set +e
set +o posix
# various alias/unalias errors
unalias
# at some point, this may mean to `export' an alias, like ksh, but
# for now it is an error
alias -x foo=barz
unalias -x fooaha
alias hoowah
unalias hoowah
# the iteration variable must be a valid identifier
for 1 in a b c
do
echo $1
done
for f\1 in a b c ; do echo $f ; done
# in posix mode, it's a fatal error
(set -o posix
for invalid-name in a b c; do echo $1; done; echo after posix for)
(set -o posix
for f\1 in a b c ; do echo $f ; done; echo after posix for 2)
# same with select
select 1 in a b c; do echo $REPLY; done
select f\1 in a b c ; do echo $REPLY ; done
select invalid-name in a b c; do echo $REPLY; done
(set -o posix ; select 1 in a b c; do echo $REPLY; done; echo after posix select)
(set -o posix ; select f\1 in a b c ; do echo $REPLY ; done ; echo after posix select 2)
# even in functions
bad-select()
{
select $1 in a b c ; do echo $REPLY ; done
}
bad-select 'a b'
unset -f bad-select
# try to rebind a read-only function
func()
{
echo func
}
readonly -f func
# make sure `readonly' and `declare' play well together
declare -Fr
func()
{
echo bar
}
# bad option
unset -x func
# cannot unset readonly functions or variables
unset -f func
# or make them not readonly
declare -fr func
declare -f +r func
# cannot use declare -f in combination with other attributes
a() { echo a; }
declare -f -a a
declare -f -i b c
XPATH=$PATH
declare -r XPATH
unset -v XPATH
# cannot unset invalid identifiers
unset /bin/sh
# cannot unset function and variable at the same time
unset -f -v SHELL
# bad option
declare -z
# cannot declare invalid identifiers
declare -- -z
declare /bin/sh
# this is the syntax used to export functions in the environment, but
# it cannot be used with `declare'
declare -f func='() { echo "this is func"; }'
# bad option to exec -- this should not exit the script
exec -i /bin/sh
# trying to exec non-executable file is a fatal error
( exec ./errors1.sub 2>/dev/null ; echo after failed exec )
# try to export -f something that is not a function -- this should be
# an error, not create an `invisible function'
export -f XPATH
# this depends on the setting of BREAK_COMPLAINS in config.h.in
break
continue
# this should not exit the shell; it did in versions before 2.01
shift label
# other shells do not complain about the extra arguments; maybe someday
# we won't either
set -- a b c
shift $# label
# and get rid of the positional parameters
shift $#
# let without an expression is an error, though maybe it should just return
# success
let
# local outside a function is an error
local
# logout of a non-login shell is an error
logout
# try to hash a non-existent command
hash notthere
# bad option to hash, although it may mean `verbose' at some future point
hash -v
# hash -d requires an argument
hash -d
# turn off hashing, then try to hash something
set +o hashall
hash -p ${THIS_SH} ${THIS_SH##*/}
declare -a AA
unset AA[-2]
# try to assign to a readonly array
declare -r AA
AA=( one two three )
# make sure `readonly -n' doesn't turn off readonly status
readonly -n AA
AA=(one two three)
# try to assign a readonly array with bad assignment syntax
# NOTE: this works in post-bash-2.05 (at least when I write this)
# readonly -a ZZZ=bbb
# bad counts to `shift'
shopt -s shift_verbose
shift $(( $# + 5 ))
shift -2
shift -- $(( $# + 5 ))
shift -- -2
# bad shell options
shopt -s no_such_option
shopt no_such_option
shopt -s -o no_such_option
# non-octal digits for umask and other errors
umask 09
umask -S u=rwx:g=rwx:o=rx >/dev/null # 002
umask -S u:rwx,g:rwx,o:rx >/dev/null # 002
# at some point, this may mean `invert', but for now it is an error
umask -i
# bad assignments shouldn't change the umask
mask=$(umask)
umask g=p
mask2=$(umask)
if [ "$mask" != "$mask2" ]; then
echo "umask errors change process umask"
fi
# assignment to a readonly variable in environment
VAR=4
readonly VAR
VAR=7 :
# more readonly variable tests
declare VAR=88
declare +r VAR
declare -p unset
# iteration variable in a for statement being readonly
for VAR in 1 2 3 ; do echo $VAR; done
# parser errors; caught early so we have to run them in subshells
${THIS_SH} -c ': $( for z in 1 2 3; do )' comsub
${THIS_SH} -c ': $( for z in 1 2 3; done )' comsub
# various `cd' errors
( unset HOME ; cd )
( HOME=/tmp/xyz.bash ; cd )
# errors from cd
cd -
cd /bin/sh # error - not a directory
OLDPWD=/tmp/cd-notthere
cd -
# too many arguments
cd one two three
# cd doesn't like it if PWD is readonly
${THIS_SH} -c 'readonly PWD ; cd / ; echo $?' bash
# or if OLDPWD is readonly
${THIS_SH} -c 'readonly OLDPWD ; cd / ; echo $?' bash
# various `source/.' errors
.
source
# maybe someday this will work like in rc
. -i /dev/tty
# make sure that this gives an error rather than setting $1
set -q
# enable non-builtins
enable sh bash
# try to set and unset shell options simultaneously
shopt -s -u checkhash
# error
read -x
# this is an error -- bad timeout spec
read -t var < /dev/null
# try to read into an invalid identifier
read /bin/sh < /dev/null
read A /bin/sh < /dev/null
read -a invalid-name < /dev/null
# try to read into a readonly variable
read VAR < /dev/null
# invalid file descriptor
read -u XX < /dev/null
read -u 42 < /dev/null
# same with mapfile
mapfile -u XX A < /dev/null
mapfile -u 42 A < /dev/null
unset -v A
# invalid identifier arguments to mapfile
mapfile '' </dev/null
mapfile invalid-var < /dev/null
# bad option to readonly/export
readonly -x foo
# someday these may mean something, but for now they're errors
eval -i "echo $-"
command -i "echo $-"
# this caused a core dump in bash-2.01 (fixed in bash-2.01.1)
eval echo \$[/bin/sh + 0]
eval echo '$((/bin/sh + 0))'
# error to list trap for an unknown signal
trap -p NOSIG
# maybe someday trap will take a -s argument like kill, but not now
trap -p -s NOSIG
# we have a ksh-like ERR trap, post-bash-2.05
#trap 'echo [$LINENO] -- error' ERR
# can only return from a function or sourced script
return 2
# break and continue with arguments <= 0
for z in 1 2 3; do
break 0
echo $x
done
for z in 1 2 3; do
continue 0
echo $x
done
# invalid option
builtin -x
# builtin with non-builtin
builtin bash
# maybe someday you will be able to use fg/bg when job control is not really
# active, but for now they are errors
bg
fg
# argument required
kill
kill -s
# bad argument
kill -S
# null argument
kill -INT ''
# argument required
kill -INT
# bad signal specification
kill -l SIGBAD
# bad signal specification
kill -l BAD
# bad process specification
kill -HUP @12
# cannot unset non-unsettable variables
unset -v BASH_LINENO BASH_SOURCE
# bad shell option names
set -o trackall # bash is not ksh
set -q # this is an error
set -i # this is not allowed
# problem with versions through bash-4.2
readonly xx=5
echo $((xx=5))
echo $?
${THIS_SH} ./errors1.sub
${THIS_SH} ./errors2.sub
${THIS_SH} ./errors3.sub
${THIS_SH} ./errors4.sub
${THIS_SH} -o posix ./errors4.sub
${THIS_SH} ./errors5.sub
${THIS_SH} ./errors6.sub
THIS_SH="${THIS_SH} -o posix" ${THIS_SH} ./errors6.sub
${THIS_SH} ./errors7.sub
${THIS_SH} -o posix ./errors7.sub
${THIS_SH} ./errors8.sub
${THIS_SH} ./errors9.sub
# invalid numeric arguments and too many arguments
${THIS_SH} ./errors10.sub
# invalid identifiers to readonly/export
${THIS_SH} ./errors11.sub
# EOF when parsing compound commands
${THIS_SH} ./errors12.sub
${THIS_SH} -c 'return ; echo after return' bash
${THIS_SH} -o posix -c 'return ; echo after return' bash
# various posix-mode special builtin fatal (or not) errors
# posix says unsetting readonly variables is a fatal error
${THIS_SH} -o posix -c 'readonly a=a ; unset -v a; echo after unset 1' sh
# the same with non-identifiers
${THIS_SH} -o posix -c 'unset -v a-b; echo after unset 2' sh
# and sourcing a non-existent file is fatal too
${THIS_SH} -o posix -c '. /nosuchfile ; echo after source' sh
# but trap specifying a bad signal nunber is non-fatal
${THIS_SH} -o posix -c 'trap "echo bad" SIGNOSIG; echo after trap' sh
# in posix mode, this is no longer a fatal error
# a function name does not have to be a valid identifier
set -o posix
function !! () { fc -s "$@" ; }
set +o posix
echo end
|