sh.test 28 KB

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