xref: /aosp_15_r20/external/toybox/tests/sh.test (revision cf5a6c84e2b8763fc1a7db14496fd4742913b199)
1#!/bin/echo no
2
3[ -f testing.sh ] && . testing.sh
4
5# TODO make fake pty wrapper for test infrastructure
6
7# testing "name" "command" "result" "infile" "stdin"
8# texpect "name" "command" [R]I/O/E"string" X[ERR]
9
10# Use "bash" name for host, "sh" for toybox. (/bin/sh is often defective.)
11[ -z "$SH" ] && { [ -z "$TEST_HOST" ] && SH="sh" || export SH="bash" ; }
12# The expected prompt is different for root vs normal user
13[ $UID -eq 0 ] && P='# ' || P='$ '
14# insulate shell child process to get predictable results
15SS="env -i PATH=${PATH@Q} PS1='\\$ ' $SH --noediting --noprofile --norc -is"
16
17[ -z "$TEST_HOST" ] && : ${BROKEN=true}
18# Wrap txpect for shell testing
19shxpect() {
20  X="$1"
21  shift
22  txpect "$X" "$SS" E"$P" "$@" X0
23}
24
25shxpect "prompt and exit" I$'exit\n'
26shxpect "prompt and echo" I$'echo hello\n' O$'hello\n' E"$P"
27shxpect "redir" I$'cat<<<hello\n' O$'hello\n' E"$P"
28shxpect "redirect err" I$'echo > /dev/full\n' E E"$P" X1
29shxpect "wait for <(exit)" I$'cat <(echo hello 1>&2)\n' E$'hello\n' E"$P"
30
31# Test shell command line (-c and how scripts run) before changing EVAL
32testing '-c "" exit status 0' '$SH -c "" && echo $?' '0\n' '' ''
33testing '-c args' "\$SH -c 'echo \$0,\$1,\$2,\$3' one two three four five" \
34  "one,two,three,four\n" "" ""
35testing '-c args2' "\$SH -c 'echo \${10}' a b c d e f g h i j k l" "k\n" "" ""
36testing '-c arg split' \
37  "$SH -c 'for i in a\"\$@\"b;do echo =\$i=;done;echo \$0' 123 456 789" \
38  "=a456=\n=789b=\n123\n" "" ""
39testing '-c arg split2' \
40  "$SH -c 'for i in a\"\$* \$@\"b; do echo =\$i=;done' one two three four five"\
41  "=atwo three four five two=\n=three=\n=four=\n=fiveb=\n" "" ""
42testing '-c arg count' "$SH -c 'echo \$#' 9 8 7 6 1 2 3 4" "7\n" "" ""
43testing 'trailing \' "$SH -c 'echo \'" '\\\n' '' ''
44testing "trailing \\ in ''" "$SH -c \$'echo \\'one\\\\\\ntwo\\''" \
45  'one\\\ntwo\n' '' ''
46testing 'trailing \ in ""' "$SH -c \$'echo \"one\\\\\\ntwo\"'" 'onetwo\n' \
47  '' ''
48testing 'vanishing \' "$SH -c \$'echo \\\\\\n a'" 'a\n' '' ''
49testing 'command\ arg' "$SH -c \$'echo\\\\\\n  abc'" 'abc\n' '' ''
50testing "exec3" '$C -c "{ exec readlink /proc/self/fd/0;} < /proc/self/exe"' \
51  "$(readlink -f $C)\n" "" ""
52testing 'arg shift' "$SH -c '"'for i in "" 2 1 1 1; do echo $? $1; shift $i; done'"' one two three four five" \
53  "0 two\n0 three\n0 five\n0\n1\n" "" ""
54testing '(subshell)' '$SH -c "(echo hello)"' 'hello\n' '' ''
55testing 'syntax' '$SH -c "if true; then echo hello | fi" 2>/dev/null || echo x'\
56  'x\n' '' ''
57testing 'syntax2' '$SH -c "for;i 0" 2>&1 | { grep -qi syntax && echo yes; }' \
58  'yes\n' '' ''
59
60# The bash man page is lying when it says $_ starts with an absolute path.
61ln -s "$(which $SH)" bash
62testing 'non-absolute $_' "./bash -c 'echo \$_'" './bash\n' '' ''
63rm bash
64
65testing 'exec exitval' "$SH -c 'exec echo hello' && echo \$?" "hello\n0\n" "" ""
66testing 'simple script' '$SH input' 'input\n' 'echo $0' ''
67testing 'simple script2' '$SH ./input two;echo $?' './input+two\n42\n' \
68  '\necho $0+$1\n\nexit 42' ''
69# this segfaults bash
70toyonly testing 'recursion guard' \
71  '$SH input 2>/dev/null; [ $? -lt 128 ] && echo pass' 'pass\n' \
72  'source input' ''
73testing '$LINENO 1' "$SH input" "1\n" 'echo $LINENO' ''
74
75mkdir sub
76echo echo hello > sub/script
77$BROKEN testing 'simple script in $PATH' "PATH='$PWD/sub:$PATH' $SH script" \
78  'hello\n' '' ''
79rm -rf sub
80
81testing "script file" "chmod +x input; ./input" "hello\n" "#!$C\necho hello" ""
82testing 'IFS $*' "$SH -c 'IFS=xy; echo \"\$*\"' one two tyree" "twoxtyree\n" \
83  "" ""
84# Without the \n\n bash 5 emits SHLVL=0
85testing 'default exports' \
86  "env -i \"$(which $SH)\" --noprofile --norc -c \$'env\n\n' | sort" \
87  "PWD=$(pwd)\nSHLVL=1\n_=$(which env)\n" "" ""
88# toysh order of operations not matching bash
89#testing "leading assignment fail" \
90#  "{ \$SH -c 'X=\${a?blah} > walroid';ls walroid;} 2>/dev/null" '' '' ''
91testing "lineno" "$SH input" "5 one\n6 one\n5 two\n6 two\n" \
92  '#!/bin/bash\n\nfor i in one two\ndo\n  echo $LINENO $i\n  echo $LINENO $i\ndone\n' ""
93testing "eval0" "$SH -c 'eval echo \$*' one two three" "two three\n" "" ""
94
95#########################################################################
96# Change EVAL to call sh -c for us, using "bash" explicitly for the host.
97export EVAL="timeout 10 $SH -c"
98
99# From here on, tests run within the new shell by default.
100
101testing 'return code' 'if false; then echo true; fi; echo $?' '0\n' '' ''
102testing 'return code 2' 'if true; then false; fi; echo $?' '1\n' '' ''
103testing 'return code 3' 'x=0; while [ $((x++)) -lt 2 ]; do echo $x; done; echo $?' '1\n2\n0\n' '' ''
104testing 'return code 4' 'false; A=B; echo $?' '0\n' '' ''
105testing 'local var +whiteout' \
106  'l=X;x(){ local l=47; echo $l;unset l; echo l=$l;};x;echo $l' '47\nl=\nX\n' \
107  '' ''
108testing 'escape passthrough' 'echo -e "a\nb\nc\td"' 'a\nb\nc\td\n' '' ''
109
110testing 'trailing $ is literal' 'echo $' '$\n' '' ''
111testing 'work after HERE' $'cat<<0;echo hello\npotato\n0' 'potato\nhello\n' '' ''
112testing '<<\EOF' $'(cat<<EOF\n$PATH\nEOF\necho "$PATH") | sort -u | wc -l' \
113  '1\n' '' ''
114testing "<<EOF''" $'(cat<<EOF\'\'\n$PATH\nEOF\necho "$PATH") | sort -u | wc -l'\
115  '2\n' '' ''
116testing '<<\EOF' $'(cat<<\\EOF\n$PATH\nEOF\necho "$PATH") | sort -u | wc -l' \
117  '2\n' '' ''
118testing '<< \' $'cat<<EOF\nabc\\\ndef\nEOF\n' 'abcdef\n' '' ''
119testing '<< "\"' $'cat<<\\EOF\nabc\\\ndef\nEOF\n' 'abc\\\ndef\n' '' ''
120testing '<<""' $'cat<<"";echo hello\npotato\n\necho huh' 'potato\nhello\nhuh\n'\
121  '' ''
122$BROKEN testing '<< trailing \' $'cat<<EOF 2>/dev/null\nabcde\nnext\\\nEOF\nEOF' \
123  'abcde\nnextEOF\n' '' ''
124$BROKEN testing '<< trailing \ 2' $'cat<<EOF\nabcde\nEO\\\nF\necho hello' \
125  'abcde\nhello\n' '' ''
126testing '<< $(a)' $'cat<<$(a)\nx\n$(a)' 'x\n' '' ''
127testing 'HERE straddle' $'cat<<EOF;if true\nhello\nEOF\nthen echo also; fi' \
128  'hello\nalso\n' '' ''
129$BROKEN testing '\\n in <<EOF' $'cat<<EO\\\nF\n$PATH\nEOF\n' "$PATH\n" "" ""
130testing '\\n in <<EOF with ""' $'cat<<EO\\\nF""\n$PATH\nEOF\n' '$PATH\n' '' ''
131$BROKEN testing '\\n in HERE terminator' $'cat<<EOF\nabc\nE\\\nOF\necho hello\n' \
132  'abc\nhello\n' '' ''
133ln -s "$(which echo)" echo2
134testing "undelimited redirect doesn't eat prefix" './echo2</dev/null hello' \
135  'hello\n' '' ''
136rm -f echo2
137testing 'prefix assignment is local' '{ echo $ABC; } {ABC}</dev/null; echo $3'\
138  '10\n\n' '' ''
139testing 'double quotes' 'echo \x "\x" "\\" "\$" "\`"' 'x \x \ $ `\n' '' ''
140testing 'quoted line escapes' $'echo "\\\none" \'\\\ntwo\'' 'one \\\ntwo\n' '' ''
141testing 'escape makes argument' 'echo \  | wc -c' '2\n' '' ''
142
143# TODO testing 'empty +() is literal' 'echo +()' '+()\n' '' ''
144
145# shxpect "EOF" I$'<<EOF;echo hello'
146shxpect 'queued work after HERE' I$'<<0;echo hello\n' E"> " I$'0\n' O$'hello\n'
147shxpect '$_ preserved on assignment error' I$'true hello; a=1 b=2 c=${}\n' \
148  E E"$P" I$'echo $_\n' O$'hello\n'
149shxpect '$_ preserved on prefix error' I$'true hello; a=1 b=2 c=${} true\n' \
150  E E"$P" I$'echo $_\n' O$'hello\n'
151shxpect '$_ preserved on exec error' I$'true hello; ${}\n' \
152  E E"$P" I$'echo $_\n' O$'hello\n'
153shxpect '$_ abspath on exec' I$'env | grep ^_=\n' O$'_=/usr/bin/env\n'
154testing '$_ literal after exec' 'env >/dev/null; echo $_' 'env\n' '' ''
155shxpect '$_ no path for builtin' I$'true; echo $_\n' O$'true\n'
156$BROKEN testing 'prefix is local for builtins' 'abc=123; abc=def unset abc; echo $abc' \
157  '123\n' '' ''
158$BROKEN testing 'prefix localizes magic vars' \
159  'SECONDS=123; SECONDS=345 true; echo $SECONDS' '123\n' '' ''
160shxpect 'body evaluated before variable exports' I$'a=x${} y${}\n' RE'y${}' X1
161testing '$NOTHING clears $_' 'true; $NOTHING; echo $_' '\n' '' ''
162testing 'assignment with redirect is persistent, not prefix' \
163  'ABC=DEF > potato && rm potato && echo $ABC' 'DEF\n' '' ''
164$BROKEN testing '$_ with functions' 'true; x(){ echo $_;}; x abc; echo $_' \
165  'true\nabc\n' '' ''
166
167mkdir -p one/two/three
168testing 'cd in renamed dir' \
169  'cd one/two/three && mv ../../../{one,four} && cd .. && echo ${PWD: -9:9}' \
170  '/four/two\n' '' ''
171rm -rf one
172
173testing "smoketest" "echo hello" "hello\n" "" ""
174testing "assignment" 'x=y; echo $x' 'y\n' '' ''
175testing "+= assignment" 'x+=abc; y=def; y+=$x; echo $y' 'defabc\n' '' ''
176testing "eval" "eval echo hello" "hello\n" "" ""
177testing "eval2" "eval 'echo hello'; echo $?" "hello\n0\n" "" ""
178testing "eval3" 'X="echo hello"; eval "$X"' "hello\n" "" ""
179testing "eval4" 'eval printf '=%s=' \" hello \"' "= hello =" "" ""
180NOSPACE=1 testing "eval5" 'eval echo \" hello \" | wc' ' 1 1 8' "" ""
181$BROKEN testing 'eval6' $'false; eval \'echo $?\'' '1\n' '' ''
182testing 'eval7' $'eval \'false\'; echo $?' '1\n' '' ''
183testing 'eval8' $'false; eval ''; echo $?' '0\n' '' ''
184$BROKEN testing 'eval9' $'A=echo; false; eval \'$A $?\'' '1\n' '' ''
185testing "exec" "exec echo hello" "hello\n" "" ""
186testing "exec2" "exec echo hello; echo $?" "hello\n" "" "" 
187
188# ; | && ||
189testing "semicolon" "echo one;echo two" "one\ntwo\n" "" ""
190testing "simple pipe" "echo hello | cat" "hello\n" "" ""
191testing "&&" "true && echo hello" "hello\n" "" ""
192testing "&&2" "false && echo hello" "" "" ""
193testing "||" "true || echo hello" "" "" ""
194testing "||2" "false || echo hello" "hello\n" "" ""
195testing "&& ||" "true && false && potato || echo hello" "hello\n" "" ""
196testing "&& after function" "x(){ false;};x && echo yes" "" "" ""
197testing "|| after function" "x(){ false;};x || echo yes" "yes\n" "" ""
198
199# redirection
200
201testing "redir1" "cat < input" "hello\n" "hello\n" ""
202testing "redir2" "echo blah >out; cat out" "blah\n" "" ""
203testing "redir3" "echo more >>out; cat out" "blah\nmore\n" "" ""
204testing "redir4" "touch /not/exist 2>out||grep -o /not/exist out" \
205  "/not/exist\n" "" ""
206testing "redir5" "ls out /not/exist &> out2 || wc -l < out2" "2\n" "" ""
207testing "redir6" "ls out /not/exist &>>-abc || wc -l < ./-abc" "2\n" "" ""
208testing "redir7" "ls out /not/exist |& wc -l" "2\n" "" ""
209testing "redir8" 'echo -n $(<input)' "boing" "boing\n" ""
210shxpect "redir9" I$'echo hello > out 2>/does/not/exist\n' E E"$P" \
211  I$'wc -l < out\n' O$'0\n'
212testing "redir10" 'echo hello 3<&3' "hello\n" "" ""
213testing "redir11" 'if :;then echo one;fi {abc}<input; cat <&$abc' \
214  "one\npotato\n" "potato\n" ""
215rm -f out out2 ./-abc
216
217# expansion
218
219testing "tilde expansion" "echo ~" "$HOME\n" "" ""
220testing "tilde2" "echo ~/dir" "$HOME/dir\n" "" ""
221testing "bracket expansion" \
222  "echo {A{a,b}B{c,d}C}" "{AaBcC} {AaBdC} {AbBcC} {AbBdC}\n" "" ""
223testing "brackets2" "echo {A{a,b}B{c,d}C,D}" "AaBcC AaBdC AbBcC AbBdC D\n" "" ""
224testing "brackets3" 'echo {A"b,c"D}' "{Ab,cD}\n" "" ""
225testing "brackets4" 'echo {A"bc",D}' "Abc D\n" "" ""
226testing "brackets5" 'echo {A,B,C' "{A,B,C\n" "" ""
227testing "brackets6" 'echo {{{{A,B},C}D},E}' "{AD} {BD} {CD} E\n" "" ""
228testing "brackets7" 'echo {{{a,b},c,{d,e}},f}' "a b c d e f\n" "" ""
229testing "brackets8" 'echo A{a{b,c{B,C}D}d{e,f},g{h,i}j}E' \
230  "AabdeE AabdfE AacBDdeE AacBDdfE AacCDdeE AacCDdfE AghjE AgijE\n" "" ""
231testing "brackets9" 'echo A{B{C,D}E{N,O},F{G,H}I}J{K,L}M' \
232  "ABCENJKM ABCENJLM ABCEOJKM ABCEOJLM ABDENJKM ABDENJLM ABDEOJKM ABDEOJLM AFGIJKM AFGIJLM AFHIJKM AFHIJLM\n" "" ""
233for i in /root /var/root /; do [ -e $i ] && EXPECT=$i && break; done
234testing "bracket+tilde" "echo {~,~root}/pwd" "$HOME/pwd $EXPECT/pwd\n" "" ""
235
236# Slices
237
238testing '${x#prefix}' 'x=abcde; echo ${x#abc}' 'de\n' '' ''
239testing '${x#short} ${x##long}' 'x=banana; echo ${x#b*n} ${x##b*n}' \
240  'ana a\n' '' ''
241toyonly testing '${x#utf8}' 'x=aそcde; echo ${x##a?c}' 'de\n' '' ''
242testing '${x%y}' 'x=potato; echo ${x%t*o} ${x%%t*o}' 'pota po\n' '' ''
243testing 'x=${x%y}' 'x=potato; x=${x%t*o}; echo $x' 'pota\n' '' ''
244testing '${x%glob}' 'x=abc-def; echo ${x%-*f} ${x-*c}' 'abc abc-def\n' '' ''
245testing 'x=${x//y}' 'x=potato; x=${x//ta}; echo $x' 'poto\n' '' ''
246testing '${x^y}' 'x=aaaaa; echo ${x^a}' 'Aaaaa\n' '' ''
247testing '${x^^y}' 'x=abccdec; echo ${x^^c}; x=abcdec; echo ${x^^c}' \
248  'abCCdeC\nabCdeC\n' '' ''
249testing '${x,y}' 'x=BBB; echo ${x,B}' 'bBB\n' '' ''
250testing '${x,,y}' 'x=POTATO; echo ${x,,} ${x,,?} ${x,,*} ${x,,T}' \
251  'potato potato potato POtAtO\n' '' ''
252
253mkdir -p abc/def/ghi
254touch www
255testing 'wildcards' 'echo w[v-x]w w[x-v]w abc/*/ghi' \
256  'www w[x-v]w abc/def/ghi\n' '' ''
257testing 'hidden wildcards' \
258  'touch .abc abc && echo *bc && echo and && echo .*bc' \
259  'abc\nand\n.abc\n' '' ''
260
261testing "backtick1" 'x=fred; echo `echo $x`' 'fred\n' "" ""
262testing "backtick2" 'x=fred; echo `x=y; echo $x`; echo $x' 'y\nfred\n' "" ""
263testing '$(( ) )' 'echo ab$((echo hello) | tr e x)cd' "abhxllocd\n" "" ""
264$BROKEN testing '$((x=y)) lifetime' 'a=boing; echo $a $a$((a=4))$a $a' 'boing boing44 4\n' '' ''
265
266testing 'quote' "echo \"'\"" "'\n" "" ""
267
268testing "math" 'echo $((1+2))' '3\n' '' ''
269testing "[oldmath]" 'echo $[1+2]' '3\n' '' ''
270testing "math basic priority" 'echo $((1+2*3**4))' '163\n' '' ''
271testing "math paren" 'echo $(((1+2)*3))' '9\n' '' ''
272testing "math spaces" 'echo $(( ( 1 + 2 ) * 7 - 5 ** 2 ))' '-4\n' '' ''
273testing "((<)) isn't redirect" '((1<2)) </dev/null && echo yes' 'yes\n' '' ''
274testing "((>)) isn't redirect" '((1>2)) </dev/null || echo yes' 'yes\n' '' ''
275testing "((not math) )" '((echo hello) )' 'hello\n' '' ''
276testing "preincrement" 'echo $((++x)); echo $x' '1\n1\n' '' ''
277testing "preincrement vs prefix plus" 'echo $((+++x)); echo $x' '1\n1\n' '' ''
278testing "predecrement" 'echo $((--x)); echo $x' '-1\n-1\n' '' ''
279testing "predecrement vs prefix minus" 'echo $((---x)); echo $x' '1\n-1\n' '' ''
280testing "minus-minus-minus" 'echo $((x---7)); echo $x' '-7\n-1\n' '' ''
281testing "x---y is x-- -y not x- --y" 'x=1 y=1; echo $((x---y)) $x $y' '0 0 1\n'\
282  '' ''
283$BROKEN testing "nesting ? :" \
284  'for((i=0;i<8;i++)); do echo $((i&1?i&2?1:i&4?2:3:4));done' \
285  '4\n3\n4\n1\n4\n2\n4\n1\n' '' ''
286testing "inherited assignment suppression" 'echo $((0 ? (x++) : 2)); echo $x' \
287  "2\n\n" "" ""
288testing "boolean vs logical" 'echo $((2|4&&8))' '1\n' '' ''
289testing "&& vs || priority" \
290  'echo $((w++||x++&&y++||z++)) w=$w x=$x y=$y z=$z' \
291  '0 w=1 x=1 y= z=1\n' '' ''
292testing "|| vs && priority" \
293  'echo $((w++&&x++||y++&&z++)) w=$w x=$x y=$y z=$z' \
294  '0 w=1 x= y=1 z=\n' '' ''
295$BROKEN shxpect '/0' I$'echo $((1/0)); echo here\n' E E"$P" I$'echo $?\n' O$'1\n'
296$BROKEN shxpect '%0' I$'echo $((1%0)); echo here\n' E E"$P" I$'echo $?\n' O$'1\n'
297$BROKEN shxpect '/=0' I$'echo $((x/=0)); echo here\n' E E"$P" I$'echo $?\n' O$'1\n'
298$BROKEN shxpect '%=0' I$'echo $((x%=0)); echo here\n' E E"$P" I$'echo $?\n' O$'1\n'
299
300# Loops and flow control
301testing "case" 'for i in A C J B; do case "$i" in A) echo got A ;; B) echo and B ;; C) echo then C ;; *) echo default ;; esac; done' \
302  "got A\nthen C\ndefault\nand B\n" "" ""
303testing 'case;;&' 'case wow in w?w) echo ok;;& wow) echo no; esac' 'ok\nno\n' \
304  "" ""
305testing "case newlines" \
306  $'case i\n\nin\n\na) echo one\n\n;;\n\ni)\n\necho two\n\n;;\n\nesac' \
307  "two\n" "" ""
308testing "case block" \
309  $'case X in\n  X) printf %s "X" || { echo potato;} ;;\nesac' 'X' '' ''
310testing 'loop in && ||' \
311  'false && for i in a b c; do echo $i; done || echo no' 'no\n' '' ''
312testing "continue" 'for i in a b c; do for j in d e f; do echo $i $j; continue 2; done; done' \
313  "a d\nb d\nc d\n" "" ""
314$BROKEN testing "piped loops that don't exit" \
315  'while X=$(($X+1)); do echo $X; done | while read i; do echo $i; done | head -n 5' \
316  '1\n2\n3\n4\n5\n' '' ''
317
318# <glinda>A variable occurred</glinda>
319
320testing "expand" 'echo $PWD' "$(pwd)\n" "" ""
321testing "expand2" 'echo "$PWD"' "$(pwd)\n" "" ""
322testing "expand3" 'echo "$"PWD' '$PWD\n' "" ""
323testing "expand4" 'P=x; echo "$P"WD' 'xWD\n' "" ""
324testing "dequote" "echo one 'two' ''three 'fo'ur '\\'" \
325  'one two three four \\\n' '' ''
326
327testing "leading variable assignment" 'abc=def env | grep ^abc=; echo $abc' \
328  "abc=def\n\n" "" ""
329testing "leading variable assignments" \
330  "abc=def ghi=jkl env | egrep '^(abc|ghi)=' | sort; echo \$abc \$ghi" \
331  "abc=def\nghi=jkl\n\n" "" ""
332$BROKEN testing "leading assignment occurs after parsing" \
333  'abc=def; abc=ghi echo $abc' "def\n" "" ""
334testing "leading assignment space" 'X="abc  def"; Y=$X; echo "$Y"' \
335  "abc  def\n" "" ""
336testing "leading assignment space2" \
337  'chicken() { X="$@"; }; chicken a b c d e; echo "$X"' 'a b c d e\n' '' ''
338testing "leading assignment fail2" \
339  "{ 1blah=123 echo hello;} 2>/dev/null || echo no" "no\n" "" ""
340testing "leading assignment redirect" \
341  "blah=123 echo hello > walrus && ls walrus" "walrus\n" "" ""
342rm -f walrus
343
344testing "{1..5}" "echo {1..5}" "1 2 3 4 5\n" "" ""
345testing "{5..1}" "echo {5..1}" "5 4 3 2 1\n" "" ""
346testing "{5..1..2}" "echo {5..1..2}" "5 3 1\n" "" ""
347testing "{a..z..-3}" "echo {a..z..-3}" "a d g j m p s v y\n" "" ""
348
349mkfifo POIT
350testing 'background curly block' \
351  '{ sed s/ll/xx/ POIT; }& echo hello > POIT; wait && echo yes' \
352  'hexxo\nyes\n' '' ''
353rm -f POIT
354
355$BROKEN testing 'background pipe block' \
356  'if true; then { sleep .25;bzcat "$FILES"/blkid/ntfs.bz2; }& fi | wc -c' \
357  '8388608\n' '' ''
358$BROKEN testing 'background variable assignment' 'X=x; X=y & echo $X' 'x\n' '' ''
359
360#$ IFS=x X=xyxz; for i in abc${X}def; do echo =$i=; done
361#=abc=
362#=y=
363#=zdef=
364
365testing "IFS whitespace before/after" \
366  'IFS=" x"; A=" x " B=" x" C="x " D=x E="   "; for i in $A $B $C $D L$A L$B L$C L$D $A= $B= $C= $D= L$A= L$B= L$C= L$D=; do echo -n {$i}; done' \
367  "{}{}{}{}{L}{L}{L}{L}{}{=}{}{=}{}{=}{}{=}{L}{=}{L}{=}{L}{=}{L}{=}" "" ""
368testing "quotes and whitespace" \
369  'A="   abc   def   "; for i in ""$A""; do echo =$i=; done' \
370  "==\n=abc=\n=def=\n==\n" "" ""
371testing "quotes and whitespace2" \
372  'A="   abc   def   "; for i in """"$A""; do echo =$i=; done' \
373  "==\n=abc=\n=def=\n==\n" "" ""
374testing "quotes and whitespace3" \
375  'A="   abc   def   "; for i in ""x""$A""; do echo =$i=; done' \
376  "=x=\n=abc=\n=def=\n==\n" "" ""
377
378testing "IFS" 'IFS=x; A=abx; echo -n "$A"' "abx" "" ""
379testing "IFS2" 'IFS=x; A=abx; echo -n $A' "ab" "" ""
380testing "IFS3" 'IFS=x; echo "$(echo abx)"' "abx\n" "" ""
381testing "IFS4" 'IFS=x; echo $(echo abx)y' "ab y\n" "" ""
382testing "IFS5" 'IFS=xy; for i in abcxdefyghi; do echo =$i=; done' \
383  "=abc def ghi=\n" "" ""
384testing "curly bracket whitespace" 'for i in {$,} ""{$,}; do echo ="$i"=; done'\
385  '=$=\n=$=\n==\n' '' ''
386
387testing 'empty $! is blank' 'echo $!' "\n" "" ""
388$BROKEN testing '$! = jobs -p' 'true & [ $(jobs -p) = $! ] && echo yes' "yes\n" "" ""
389
390testing '$*' 'cc(){ for i in $*;do echo =$i=;done;};cc "" "" "" "" ""' \
391  "" "" ""
392testing '$*2' 'cc(){ for i in "$*";do echo =$i=;done;};cc ""' \
393  "==\n" "" ""
394testing '$*3... Flame. Flames. Flames, on the side of my face...' \
395  'cc(){ for i in "$*";do echo =$i=;done;};cc "" ""' "= =\n" "" ""
396$BROKEN testing 'why... oh.' \
397  'cc() { echo ="$*"=; for i in =$*=; do echo -$i-; done;}; cc "" ""; echo and; cc ""' \
398  '= =\n-=-\n-=-\nand\n==\n-==-\n' "" ""
399testing 'really?' 'cc() { for i in $*; do echo -$i-; done;}; cc "" "" ""' \
400  "" "" ""
401testing 'Sigh.' 'cc() { echo =$1$2=;}; cc "" ""' "==\n" "" ""
402testing '$*4' 'cc(){ for i in "$*";do echo =$i=;done;};cc "" "" "" "" ""' \
403  "= =\n" "" ""
404testing '$*5' 'cc(){ for i in "$*";do echo =$i=;done;};cc "" "abc" ""' \
405  "= abc =\n" "" ""
406
407# creating empty arguments without quotes
408testing '$* + IFS' \
409  'IFS=x; cc(){ for i in $*; do echo =$i=;done;};cc xabcxx' \
410  "==\n=abc=\n==\n" "" ""
411testing '$@' 'cc(){ for i in "$@";do echo =$i=;done;};cc "" "" "" "" ""' \
412  "==\n==\n==\n==\n==\n" "" ""
413testing "IFS10" 'IFS=bcd; A=abcde; for i in $A; do echo =$i=; done' \
414  "=a=\n==\n==\n=e=\n" "" ""
415$BROKEN testing "IFS11" \
416  'IFS=x; chicken() { for i in $@$@; do echo =$i=; done;}; chicken one "" abc dxf ghi' \
417  "=one=\n==\n=abc=\n=d=\n=f=\n=ghione=\n==\n=abc=\n=d=\n=f=\n=ghi=\n" "" ""
418testing "IFS12" 'IFS=3;chicken(){ return 3;}; chicken;echo 3$?3' '3 3\n' "" ""
419
420testing "IFS combinations" \
421  'IFS=" x"; A=" x " B=" x" C="x " D=x E="   "; for i in $A $B $C $D L$A L$B L$C L$D $A= $B= $C= $D= L$A= L$B= L$C= L$D=; do echo -n {$i}; done' \
422  "{}{}{}{}{L}{L}{L}{L}{}{=}{}{=}{}{=}{}{=}{L}{=}{L}{=}{L}{=}{L}{=}" "" ""
423
424$BROKEN testing "! isn't special" "echo !" "!\n" "" ""
425testing "! by itself" '!; echo $?' "1\n" "" ""
426testing "! true" '! true; echo $?' "1\n" "" ""
427testing "! ! true" '! ! true; echo $?' "0\n" "" ""
428testing "! syntax err" '! echo 2>/dev/null < doesnotexist; echo $?' "0\n" "" ""
429
430# The bash man page doesn't say quote removal here, and yet:
431testing "case quoting" 'case a in "a") echo hello;; esac' 'hello\n' "" ""
432
433testing "subshell splitting" 'for i in $(true); do echo =$i=; done' "" "" ""
434testing "subshell split 2" 'for i in $(echo "one two thr"); do echo =$i=; done'\
435  "=one=\n=two=\n=thr=\n" "" ""
436
437# variable assignment argument splitting only performed for "$@"
438testing "assignment nosplit" 'X="one two"; Y=$X; echo $Y' "one two\n" "" ""
439testing "argument splitting" \
440  'chicken() { for i in a"$@"b;do echo =$i=;done;}; chicken 123 456 789' \
441  "=a123=\n=456=\n=789b=\n" "" ""
442testing "assignment nosplit2" 'pop(){ X="$@";};pop one two three; echo $X' \
443  "one two three\n" "" ""
444
445#testing "leading assignments don't affect current line" \
446#  'VAR=12345 echo ${VAR}a' "a\n" "" ""
447#testing "can't have space before first : but yes around arguments" \
448#  'BLAH=abcdefghi; echo ${BLAH: 1 : 3 }' "bcd\n" "" ""
449
450testing "subshell exit err" '(exit 42); echo $?' "42\n" "" ""
451
452# Same thing twice, but how do we cmp if exec exited?
453#testing 'exec and $$' testing 'echo $$;exec readlink /proc/self' 
454
455X="$(realpath $(which readlink))"
456testing "exec in paren" \
457  '(exec readlink /proc/self/exe);echo hello' "$X\nhello\n" "" ""
458testing "exec in brackets" \
459  "{ exec readlink /proc/self/exe;};echo hi" "$X\n" "" ""
460
461NOSPACE=1 testing "curly brackets and pipe" \
462  '{ echo one; echo two ; } | tee blah.txt; wc blah.txt' \
463  "one\ntwo\n2 2 8 blah.txt\n" "" ""
464NOSPACE=1 testing "parentheses and pipe" \
465  '(echo two;echo three)|tee blah.txt;wc blah.txt' \
466  "two\nthree\n2 2 10 blah.txt\n" "" ""
467$BROKEN testing "pipe into parentheses" \
468  'echo hello | (read i <input; echo $i; read i; echo $i)' \
469  "there\nhello\n" "there\n" ""
470
471$BROKEN testing "\$''" $'echo $\'abc\\\'def\\nghi\'' "abc'def\nghi\n" '' ''
472testing "shift shift" 'shift; shift; shift; echo $? hello' "1 hello\n" "" ""
473testing 'search cross $*' 'chicken() { echo ${*/b c/ghi}; }; chicken a b c d' \
474  "a b c d\n" "" ""
475testing 'eval $IFS' 'IFS=x; X=x; eval abc=a${X}b 2>/dev/null; echo $abc' \
476  "\n" '' ''
477$BROKEN testing '${@:3:5}' 'chicken() { for i in "${@:3:5}"; do echo =$i=; done; } ; chicken ab cd ef gh ij kl mn op qr' \
478  '=ef=\n=gh=\n=ij=\n=kl=\n=mn=\n' '' ''
479$BROKEN testing '${*:3:5}' 'chicken() { for i in "${*:3:5}"; do unset IFS; echo =$i=; done; } ; IFS=x chicken ab cd ef gh ij kl mn op qr' \
480  '=efxghxijxklxmn=\n' '' ''
481testing 'sequence check' 'IFS=x; X=abxcd; echo ${X/bxc/g}' 'agd\n' '' ''
482
483# TODO: The txpect plumbing does not work right yet even on TEST_HOST
484#txpect "backtick0" "$SS" "E$P" 'IX=fred; echo `echo \\\\$x`'$'\n' 'Ofred' "E$P" X0
485#txpect "backtick1" "$SS" "E$P" 'IX=fred; echo `echo $x`'$'\n' 'Ofred'$'\n' "E$P" X0
486#txpect "backtick2" "$SS" "E$P" 'IX=fred; echo `x=y; echo $x`' $'Oy\n' "E$P" X0
487
488shxpect '${ with newline' I$'HELLO=abc; echo ${HELLO/b/\n' E"> " I$'}\n' O$'a c\n'
489
490testing 'here0' 'cat<<<hello' 'hello\n' '' ''
491shxpect 'here1' I$'POTATO=123; cat << EOF\n' E"> " \
492  I$'$POTATO\n' E"> " I$'EOF\n' O$'123\n'
493shxpect 'here2' I$'POTATO=123; cat << E"O"F\n' E"> " \
494  I$'$POTATO\n' E"> " I$'EOF\n' O$'$POTATO\n'
495testing 'here3' 'abc(){ cat <<< x"$@"yz;};abc one two "three  four"' \
496  "xone two three  fouryz\n" "" ""
497testing 'here4' 'for i in one two three; do cat <<< "ab${i}de"; done' \
498  'abonede\nabtwode\nabthreede\n' '' ''
499testing 'here5' $'cat << EOF && cat << EOF2\nEOF2\nEOF\nEOF\nEOF2' \
500  'EOF2\nEOF\n' '' ''
501# Nothing is actually quoted, but there are quotes, therefore...
502testing 'here6' $'cat << EOF""\n$POTATO\nEOF' '$POTATO\n' '' ''
503# Not ambiguous when split, unlike <$FILENAME redirects
504$BROKEN testing 'here7' 'ABC="abc def"; cat <<< $ABC' 'abc def\n' '' ''
505# What does HERE expansion _not_ expand?
506testing 'here8' $'ABC="x y"\ncat << EOF\n~root/{"$ABC",def}\nEOF' \
507  '~root/{"x y",def}\n' '' ''
508testing '<<- eats leading tabs before expansion, but not after' \
509  $'A=$\'\\tone\'; cat <<- EOF\n\t\t$A\n\ttwo\nEOF' "\tone\ntwo\n" '' ''
510
511testing '${var}' 'X=abcdef; echo ${X}' 'abcdef\n' '' '' 
512testing '${#}' 'X=abcdef; echo ${#X}' "6\n" "" ""
513testing 'empty ${}' '{ echo ${};} 2>&1 | grep -o bad' 'bad\n' '' ''
514$BROKEN shxpect 'empty ${} syntax err abort' I$'echo ${}; echo hello\n' \
515  E I$'echo and\n' O$'and\n'
516$BROKEN testing '${$b}' '{ echo ${$b};} 2>&1 | grep -o bad' 'bad\n' '' ''
517testing '${!PATH*}' 'echo ${!PATH*}' 'PATH\n' '' ''
518testing '${!PATH@}' 'echo ${!PATH@}' 'PATH\n' '' ''
519#testing '${!PATH[@]}' 'echo ${!PATH[@]}' '0\n' '' ''
520testing '${!x}' 'X=abcdef Y=X; echo ${!Y}' 'abcdef\n' '' ''
521testing '${!x@}' 'ABC=def; def=ghi; echo ${!ABC@}' 'ABC\n' '' ''
522$BROKEN testing '${!x} err' '{ X=abcdef Y=X:2; echo ${!Y}; echo bang;} 2>/dev/null' \
523  '' '' ''
524testing '${!x*}' 'abcdef=1 abc=2 abcq=; echo "${!abc@}" | tr " " \\n | sort' \
525  'abc\nabcdef\nabcq\n' '' ''
526testing '${!x*} none' 'echo "${!abc*}"' '\n' '' ''
527$BROKEN testing '${!x*} err' '{ echo "${!abc*x}"; echo boing;} 2>/dev/null' '' '' ''
528# TODO bash 5.x broke this
529#testing '${!none@Q}' 'echo ${X@Q} ${!X@Q}; X=ABC; echo ${!X@Q}' '\n\n' '' ''
530$BROKEN testing '${!x@Q}' 'ABC=123 X=ABC; echo ${!X@Q}' "'123'\n" '' ''
531$BROKEN testing '${#@Q}' 'echo ${#@Q}' "'0'\n" '' ''
532$BROKEN testing '${!*}' 'xx() { echo ${!*};}; fruit=123; xx fruit' '123\n' '' ''
533$BROKEN testing '${!*} indirect' 'xx() { echo ${!a@Q};}; a=@; xx one two three' \
534  "'one' 'two' 'three'\n" '' ''
535$BROKEN testing '${!x@ } match' \
536  '{ ABC=def; def=ghi; echo ${!ABC@ }; } 2>&1 | grep -o bad' 'bad\n' '' ''
537# Bash added an error for this between 4.4 and 5.x.
538#testing '${!x@ } no match no err' 'echo ${!ABC@ }def' 'def\n' '' ''
539$BROKEN testing '${!x@ } no match no err2' 'ABC=def; echo ${!ABC@ }ghi' 'ghi\n' '' ''
540toyonly testing '${#x::}' 'ABC=abcdefghijklmno; echo ${#ABC:1:2}' '5\n' '' ''
541# TODO: ${!abc@x} does _not_ error? And ${PWD@q}
542testing '$""' 'ABC=def; echo $"$ABC"' 'def\n' '' ''
543testing '"$""" does not nest' 'echo "$"abc""' '$abc\n' '' ''
544$BROKEN testing '${\}}' 'ABC=ab}cd; echo ${ABC/\}/x}' 'abxcd\n' '' ''
545testing 'bad ${^}' '{ echo ${^};} 2>&1 | grep -o bad' 'bad\n' '' ''
546shxpect '${:} empty len is err' I$'ABC=def; echo ${ABC:}\n' RE'ABC' X
547testing '${::} both empty=0' 'ABC=def; echo ${ABC::}' '\n' '' ''
548testing '${::} first empty' 'ABC=def; echo ${ABC: : 2 }' 'de\n' '' ''
549testing '${::} second empty' 'ABC=def; echo ${ABC: 2 : }' '\n' '' ''
550testing '${:}' 'ABC=def; echo ${ABC:1}' 'ef\n' '' ''
551testing '${a: }' 'ABC=def; echo ${ABC: 1}' 'ef\n' '' ''
552$BROKEN testing '${a :}' 'ABC=def; { echo ${ABC :1};} 2>&1 | grep -o bad' 'bad\n' '' ''
553testing '${::}' 'ABC=defghi; echo ${ABC:1:2}' 'ef\n' '' ''
554testing '${: : }' 'ABC=defghi; echo ${ABC: 1 : 2 }' 'ef\n' '' ''
555testing '${::} indirect' \
556  'ABC=defghi:1:2; ( echo ${!ABC};) 2>input; [ -s input ] && echo yes' \
557  'yes\n' '' ''
558testing '${::-}' 'ABC=defghi; echo ${ABC:1:-2}' 'efg\n' '' ''
559testing '${:-:-}' 'ABC=defghi; echo ${ABC:-3:2}' 'defghi\n' '' ''
560testing '${:-:-}2' 'echo ${ABC:-3:2}' '3:2\n' '' ''
561testing '${: -:}' 'ABC=defghi; echo ${ABC: -3:2}' 'gh\n' '' ''
562testing '${@%}' 'chicken() { for i in "${@%abc}"; do echo "=$i="; done;}; chicken 1abc 2abc 3abc' '=1=\n=2=\n=3=\n' '' ''
563testing '${*%}' 'chicken() { for i in "${*%abc}"; do echo "=$i="; done;}; chicken 1abc 2abc 3abc' '=1 2 3=\n' '' ''
564$BROKEN testing '${@@Q}' 'xx() { echo "${@@Q}"; }; xx one two three' \
565  "'one' 'two' 'three'\n" '' ''
566
567shxpect '${/newline/}' I$'x=$\'\na\';echo ${x/\n' E'> ' I$'/b}\n' O$'ba\n' E'> '
568
569shxpect 'line continuation' I$'echo "hello" \\\n' E'> ' I$'> blah\n' E"$P" \
570  I$'wc blah\n' O$'1 1 6 blah\n'
571shxpect 'line continuation2' I$'echo ABC\\\n' E'> ' I$'DEF\n' O$'ABCDEF\n'
572testing "line continuation3" $'ec\\\nho hello' 'hello\n' '' ''
573testing "line continuation4" $'if true | \\\n(true);then echo true;fi' 'true\n' '' ''
574$BROKEN testing "line continuation5" $'XYZ=xyz; echo "abc$\\\nXYZ"' 'abcxyz\n' '' ''
575
576# Race condition (in bash, but not in toysh) can say 43.
577$BROKEN testing 'SECONDS' 'readonly SECONDS=41; sleep 1; echo $SECONDS' '42\n' '' ''
578# testing 'SECONDS2' 'readonly SECONDS; SECONDS=0; echo $SECONDS' '' '' '' #bash!
579$BROKEN testing 'SECONDS2' 'SECONDS=123+456; echo $SECONDS' '0\n' '' '' #bash!!
580testing '$LINENO 2' $'echo $LINENO\necho $LINENO' '1\n2\n' '' ''
581testing '$EUID' 'echo $EUID' "$(id -u)\n" '' ''
582testing '$UID' 'echo $UID' "$(id -ur)\n" '' ''
583
584$BROKEN testing 'readonly leading assignment' \
585  '{ readonly abc=123;abc=def echo hello; echo $?;} 2>output; grep -o readonly output' \
586  'hello\n0\nreadonly\n' '' ''
587$BROKEN testing 'readonly leading assignment2' \
588  'readonly boink=123; export boink; { boink=234 env | grep ^boink=;} 2>/dev/null; echo $?' 'boink=123\n0\n' '' ''
589$BROKEN testing 'readonly for' \
590  'readonly i; for i in one two three; do echo $i; done 2>/dev/null; echo $?' \
591  '1\n' '' ''
592$BROKEN testing 'readonly {}<' \
593  'readonly i; echo hello 2>/dev/null {i}</dev/null; echo $?' '1\n' '' ''
594testing '$_ 1' 'echo walrus; echo $_' 'walrus\nwalrus\n' '' ''
595testing '$_ 2' 'unset _; echo $_' '_\n' '' ''
596
597# wildcards
598
599touch walrus wallpapers
600testing 'IFS wildcards' \
601  'IFS=xy; ABC=abcywal*sxdef; echo $ABC | tr " " "\n" | sort' \
602  'abc\ndef\nwallpapers\nwalrus\n' '' ''
603rm -f walrus wallpapers
604
605# Force parsing granularity via interactive shxpect because bash parses all
606# of sh -c "str" in one go, meaning the "shopt -s extglob" won't take effect
607$BROKEN shxpect 'IFS +(extglob)' I$'shopt -s extglob\n' E"$P" \
608  I$'IFS=x; ABC=cxd; for i in +($ABC); do echo =$i=; done\n' \
609  O$'=+(c=\n' O$'=d)=\n'
610
611touch abc\)d
612$BROKEN shxpect 'IFS +(extglob) 2' I$'shopt -s extglob\n' E"$P" \
613  I$'ABC="c?d"; for i in ab+($ABC); do echo =$i=; done\n' \
614  O$'=abc)d=\n'
615rm abc\)d
616
617$BROKEN shxpect '[+(]) overlap priority' I$'shopt -s extglob\n' E"$P" \
618  I$'touch "AB[DEF]"; echo AB[+(DEF]) AB[+(DEF)? AB+([DEF)]\n' \
619  O$'AB[+(DEF]) AB[DEF] AB+([DEF)]\n' \
620  I$'X="("; Y=")"; echo AB[+${X}DEF${Y}?\n' O$'AB[DEF]\n'
621
622# TODO: syntax error takes out ': ${a?b}; echo $?' (I.E. never runs echo)
623shxpect '${a?b} sets err, stops cmdline eval' \
624  I$': ${a?b} ${c:=d}\n' E E"$P" I$'echo $?$c\n' O$'1\n'
625
626$BROKEN shxpect 'trace redirect' I$'set -x; echo one\n' E$'+ echo one\n'"$P" O$'one\n' \
627  I$'echo two 2>/dev/null\n' O$'two\n' E$'+ echo two\n'"$P" \
628  I$'{ echo three; } 2>/dev/null\n' O$'three\n' E"$P"
629shxpect 'set -u' I$'set -u; echo $walrus\n' REwalrus X
630
631testing 'source file' 'source input' 'hello\n' 'echo hello \\\n' ''
632testing '. file' '. input' 'hello\n' 'echo hello \\\n' ''
633testing 'source no newline' 'source input' 'hello \\\n' 'echo hello \\' ''
634testing 'source continues' 'echo hello; source <(echo false); echo $?' \
635  'hello\n1\n' '' ''
636testing 'source returns' 'source <(echo return 37); echo $?' '37\n' '' ''
637testing 'source is live' \
638  'for i in one two three; do echo "echo $i" > input; source input; done' \
639  'one\ntwo\nthree\n' 'x' ''
640testing 'source is live in functions' \
641  'func() { source input; }; for i in one two three; do echo echo $i > input; func; done' \
642  'one\ntwo\nthree\n' 'x' ''
643testing 'subshell inheritance' \
644  'func() { source input; cat <(echo $xx; xx=456; echo $xx); echo $xx;}; echo local xx=123 > input; func; echo $xx' \
645  '123\n456\n123\n\n' 'x' ''
646$BROKEN testing 'semicolon vs newline' \
647  'source input 2>/dev/null || echo yes' 'one\nyes\n' \
648  'echo one\necho two; echo |' ''
649$BROKEN testing 'syntax err pops to source but encapsulating function continues' \
650  'func() { echo one; source <(echo -e "echo hello\necho |") 2>/dev/null; echo three;}; func; echo four' \
651  'one\nhello\nthree\nfour\n' '' ''
652$BROKEN testing '"exit shell" means exit eval but encapsulating function continues' \
653  'func() { eval "echo one; echo \${?potato}; echo and" 2>/dev/null; echo plus;}; func; echo then' \
654  'one\nplus\nthen\n' '' ''
655$BROKEN testing 'return needs function or source' \
656  'cat <(return 0 2>/dev/null; echo $?); echo after' '2\nafter\n' '' ''
657testing 'return nests' 'y(){ x; return $((3+$?));};x(){ return 5; };y;echo $?' \
658  '8\n' '' ''
659
660shxpect "functions need block" I$'x() echo;\n' RE'[Ss]yntax [Ee]rror' X2
661testing 'functions() {} in same PID' \
662  '{ echo $BASHPID; chicken() { echo $BASHPID;}; chicken;} | sort -u | wc -l' '1\n' '' ''
663testing 'functions() () different PID' \
664  '{ echo $BASHPID; chicken() ( echo $BASHPID;); chicken;} | sort -u | wc -l' '2\n' '' ''
665testing 'function() just wants any block span' \
666  'func() if true; then echo hello; fi; echo one; func; echo two' \
667  'one\nhello\ntwo\n' '' ''
668testing 'function alternate syntax' \
669  'function func if true; then echo hello; fi; echo one; func; echo two' \
670  'one\nhello\ntwo\n' '' ''
671testing 'function syntax 3' \
672  'function func ( ) if true; then echo hello; fi; echo one; func; echo two' \
673  'one\nhello\ntwo\n' '' ''
674testing 'function nested parentheses' \
675  '( potato() { echo aaa; }; potato )' 'aaa\n' '' ''
676shxpect 'local creates a whiteout' \
677  I$'func() { local potato; echo ${potato?bang}; }; potato=123; func\n' \
678  E E"$P" I$'echo $?\n' O$'1\n'
679testing 'local replaces/preserves magic type' \
680  'x() { local RANDOM=potato; echo $RANDOM;};x;echo -e "$RANDOM\n$RANDOM"|wc -l'\
681  'potato\n2\n' '' ''
682
683$BROKEN testing '$$ is parent shell' \
684  '{ echo $$; (echo $$) } | sort -u | wc -l' "1\n" "" ""
685$BROKEN testing '$PPID is parent shell' \
686  '{ echo $PPID; (echo $PPID) } | sort -u | wc -l' "1\n" "" ""
687$BROKEN testing '$BASHPID is current PID' \
688  '{ echo $BASHPID; (echo $BASHPID) } | sort -u | wc -l' "2\n" "" ""
689
690testing 'unexport supports +=' 'export -n ABC+=DEF; declare -p ABC' \
691  'declare -- ABC="DEF"\n' '' ''
692$BROKEN testing 'unexport existing +=' \
693  'export ABC=XYZ; export -n ABC+=DEF; declare -p ABC' \
694  'declare -- ABC="XYZDEF"\n' '' ''
695
696$BROKEN testing '$!' '{ echo $BASHPID & echo $!; echo ${!};} | sort -u | wc -l' '1\n' \
697  '' ''
698
699shxpect 'blank line preserves $?' \
700  I$'false\n' E"$P" I$'\n' E"$P" I$'echo $?\n' O$'1\n'
701testing 'NOP line clears $?' 'false;$NOTHING;echo $?' '0\n' '' ''
702$BROKEN testing 'run "$@"' 'false;"$@";echo $?' '0\n' '' ''
703
704# "Word splitting... not performed on the words between the [[ and ]]"
705testing '[[split1]]' 'A="1 -lt 2"; [[ $A ]] && echo yes' 'yes\n' '' ''
706testing '[[split2]]' 'A="2 -lt 1"; [[ $A ]] && echo yes' 'yes\n' '' ''
707testing '[[split3]]' \
708  'A="2 -lt 1"; [[ -e $A ]] && echo one; touch "$A" && [[ -e $A ]] && echo two'\
709  'two\n' '' ''
710rm -f '2 -lt 1'
711testing '[[split4]]' \
712  '[[ $(cat) == "a b" ]] <<< "a b" > potato && rm potato && echo ok' \
713  'ok\n' '' ''
714$BROKEN testing '[[split5]]' \
715  '[[ $(cat) == "a b" ]] < <(echo a b) > potato && rm potato && echo ok' \
716  'ok\n' '' ''
717# And token parsing leaking through: 1>2 is an error, 1 >2 is not
718testing '[[1>2]] is not a redirect' '[[ 1 >2 ]] || [ -e 2 ] || echo yup' \
719  'yup\n' '' ''
720testing "[[1 >0]] doesn't need that second space" \
721  '[[ 1 >0 ]] && { [ -e 2 ] || echo yup; }' 'yup\n' '' ''
722testing '[[1<2]] is alphabetical, not numeric' '[[ 123 < 19 ]] && echo yes' \
723  'yes\n' '' ''
724testing '[[~]]' '[[ ~ == $HOME ]] && echo yes' 'yes\n' '' ''
725
726# The trailing space is because the \n gets stripped off otherwise
727testing 'quoting contexts nest' \
728  $'echo -n "$(echo "hello $(eval $\'echo -\\\\\\ne \\\'world\\n \\\'\')")"' \
729  'hello world\n ' '' ''
730testing "\$'' suppresses variable expansion" \
731  $'echo $\'$(abc\'' '$(abc\n' '' ''
732
733testing 'if; is a syntax error but if $EMPTY; is not' \
734  'if $NONE; then echo hello; fi' 'hello\n' '' ''
735
736# TODO finish variable list from shell init
737
738# $# $? $- $! $0  # $$
739# always exported: PWD SHLVL _
740#  ./bash -c 'echo $_' prints $BASH, but PATH search shows path? Hmmm...
741# ro: UID PPID EUID $
742# IFS LINENO
743# PATH HOME SHELL USER LOGNAME SHLVL HOSTNAME HOSTTYPE MACHTYPE OSTYPE OLDPWD
744# PS0 PS1='$ ' PS2='> ' PS3 PS4 BASH BASH_VERSION
745# ENV - if [ -n "$ENV" ]; then . "$ENV"; fi # BASH_ENV - synonym for ENV
746# FUNCNEST - maximum function nesting level (abort when above)
747# REPLY - set by input with no args
748# OPTARG OPTIND - set by getopts builtin
749# OPTERR
750
751# maybe not: EXECIGNORE, FIGNORE, GLOBIGNORE
752
753#BASH_SUBSHELL - SHLVL synonym
754#BASH_EXECUTION_STRING - -c argument
755#
756#automatically set:
757#OPTARG - set by getopts builtin
758#OPTIND - set by getopts builtin
759#
760#PROMPT_COMMAND PROMPT_DIRTRIM PS0 PS1 PS2 PS3 PS4
761#
762#unsettable (assignments ignored before then)
763#LINENO SECONDS RANDOM
764#GROUPS - id -g
765#HISTCMD - history number
766#
767#TMOUT - used by read
768
769# does not match: ./sh -c 'echo {a..Z}' becomes a ` _ ^ ] \ [ Z
770
771# commit ec6639407b9e
772#-  IS_TOYBOX_RE='(toybox|This is not GNU).*'
773#-  [[ "$IS_TOYBOX" =~ $IS_TOYBOX_RE ]] || SKIPNEXT=1
774#+  case "$IS_TOYBOX" in
775#+    toybox*) ;;
776#+    This\ is\ not\ GNU*) ;;
777#+    *) SKIPNEXT=1 ;;
778#+  esac
779
780# TODO: categorize tests
781
782# TODO https://mywiki.wooledge.org/BashFAQ
783#   http://tiswww.case.edu/php/chet/bash/FAQ
784#   https://mywiki.wooledge.org/BashPitfalls#set_-euo_pipefail
785
786#        // ${#} ${#x} ${#@} ${#x[@]} ${#!} ${!#}
787#        // ${!} ${!@} ${!@Q} ${!x} ${!x@} ${!x@Q} ${!x#} ${!x[} ${!x[*]}
788
789# Looked like a prefix but wasn't: three chars (@ # -) are both paremeter name
790# and slice operator. When immediately followed by } it's parameter, otherwise
791# we did NOT have a prefix and it's an operator.
792#
793# ${#-} ${#-abc}
794# ${##} ${##0}
795# ${#@} ${#@Q}
796#
797# backslash not discarded: echo "abc\"def"
798
799# ${x:-y} use default
800# ${x:=y} assign default (error if positional)
801# ${x:?y} err if null
802# ${x:+y} alt value
803# ${x:off} ${x:off:len} off<0 from end (must ": -"), len<0 also from end must
804#   0-based indexing
805# ${@:off:len} positional parameters, off -1 = len, -len is error
806#   1-based indexing
807
808# [] wins over +()
809# touch 'AB[DEF]'; echo AB[+(DEF]) AB[+(DEF)?
810# AB[+(DEF]) AB[DEF]
811
812# Testing shell corner cases _within_ a shell script is kind of hard.
813