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