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