@@ -0,0 +1,2802 @@ |
| 1 | +#!/bin/sh |
| 2 | +# ===================================== |
| 3 | +# POSIX Compliance Gap Coverage Test Suite |
| 4 | +# ===================================== |
| 5 | +# This test file specifically targets gaps in the existing 4 POSIX test files |
| 6 | +# by comparing against IEEE Std 1003.1-2017 (POSIX.1-2017) |
| 7 | +# |
| 8 | +# Focus areas: |
| 9 | +# - Here-document variations (<<- tab stripping) |
| 10 | +# - Additional redirection operators (<>) |
| 11 | +# - Complex IFS field splitting scenarios |
| 12 | +# - Nested and complex parameter expansions |
| 13 | +# - Command name resolution order |
| 14 | +# - Complex quoting and escaping |
| 15 | +# - Pipeline exit status rules |
| 16 | +# - Complex arithmetic edge cases |
| 17 | +# - Locale and environment variables |
| 18 | +# - Builtin command edge cases |
| 19 | +# - Pathname expansion edge cases |
| 20 | +# - Function scope and recursion edge cases |
| 21 | +# - Signal handling edge cases |
| 22 | + |
| 23 | +# Colors (POSIX-compliant way) |
| 24 | +RED='\033[0;31m' |
| 25 | +GREEN='\033[0;32m' |
| 26 | +YELLOW='\033[1;33m' |
| 27 | +BLUE='\033[0;34m' |
| 28 | +NC='\033[0m' |
| 29 | + |
| 30 | +# Test identification |
| 31 | +TEST_PREFIX="[posix-gaps]" |
| 32 | +CURRENT_SECTION="" |
| 33 | +TEST_NUM=0 |
| 34 | + |
| 35 | +PASSED=0 |
| 36 | +FAILED=0 |
| 37 | +SKIPPED=0 |
| 38 | +FAILED_TESTS_LIST="" |
| 39 | +DEBUG_INFO="" |
| 40 | + |
| 41 | +# Get script directory (POSIX way) |
| 42 | +SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd) |
| 43 | +FORTSH_BIN="${FORTSH_BIN:-$SCRIPT_DIR/../bin/fortsh}" |
| 44 | +BASH_REF="${BASH_REF:-bash}" |
| 45 | + |
| 46 | +# Check if fortsh exists |
| 47 | +if [ ! -x "$FORTSH_BIN" ]; then |
| 48 | + printf "${RED}ERROR${NC}: fortsh binary not found at $FORTSH_BIN\n" |
| 49 | + printf "Please run 'make' first or set FORTSH_BIN environment variable\n" |
| 50 | + exit 1 |
| 51 | +fi |
| 52 | + |
| 53 | +# Test result trackers |
| 54 | +pass() { |
| 55 | + TEST_NUM=$((TEST_NUM + 1)) |
| 56 | + printf "${GREEN}✓ PASS${NC} ${TEST_PREFIX} ${CURRENT_SECTION}.${TEST_NUM}: %s\n" "$1" |
| 57 | + PASSED=$((PASSED + 1)) |
| 58 | +} |
| 59 | + |
| 60 | +fail() { |
| 61 | + TEST_NUM=$((TEST_NUM + 1)) |
| 62 | + TEST_ID="${TEST_PREFIX} ${CURRENT_SECTION}.${TEST_NUM}" |
| 63 | + printf "${RED}✗ FAIL${NC} ${TEST_ID}: %s\n" "$1" |
| 64 | + FAILED_TESTS_LIST="${FAILED_TESTS_LIST} ${TEST_ID}: $1\n" |
| 65 | + if [ -n "$2" ]; then |
| 66 | + printf " posix: %s\n" "$2" |
| 67 | + fi |
| 68 | + if [ -n "$3" ]; then |
| 69 | + printf " fortsh: %s\n" "$3" |
| 70 | + fi |
| 71 | + FAILED=$((FAILED + 1)) |
| 72 | +} |
| 73 | + |
| 74 | +skip() { |
| 75 | + TEST_NUM=$((TEST_NUM + 1)) |
| 76 | + printf "${YELLOW}⊘ SKIP${NC} ${TEST_PREFIX} ${CURRENT_SECTION}.${TEST_NUM}: %s - %s\n" "$1" "$2" |
| 77 | + SKIPPED=$((SKIPPED + 1)) |
| 78 | +} |
| 79 | + |
| 80 | +section() { |
| 81 | + # Extract section number from header like "91. HERE DOCUMENT VARIATIONS" |
| 82 | + CURRENT_SECTION=$(echo "$1" | grep -oE '^[0-9]+' || echo "0") |
| 83 | + TEST_NUM=0 |
| 84 | + printf "\n" |
| 85 | + printf "${BLUE}==========================================\n" |
| 86 | + printf "%s\n" "$1" |
| 87 | + printf "==========================================${NC}\n" |
| 88 | +} |
| 89 | + |
| 90 | +# Normalize shell error messages by stripping shell name and "line N: " prefix |
| 91 | +normalize_output() { |
| 92 | + sed -e 's|^[^ ]*bash: |sh: |' -e 's|^[^ ]*fortsh: |sh: |' -e 's/line [0-9]*: //' |
| 93 | +} |
| 94 | + |
| 95 | +# Helper function to run command in both shells and compare |
| 96 | +compare_posix_output() { |
| 97 | + test_name="$1" |
| 98 | + command="$2" |
| 99 | + posix_file="/tmp/posix_gaps_$$_posix" |
| 100 | + fortsh_file="/tmp/posix_gaps_$$_fortsh" |
| 101 | + |
| 102 | + # Run in POSIX shell (sh) |
| 103 | + "$BASH_REF" -c "$command" 2>&1 | normalize_output > "$posix_file" || true |
| 104 | + |
| 105 | + # Run in fortsh |
| 106 | + "$FORTSH_BIN" -c "$command" 2>&1 | normalize_output > "$fortsh_file" || true |
| 107 | + |
| 108 | + # Compare outputs |
| 109 | + if diff -q "$posix_file" "$fortsh_file" > /dev/null 2>&1; then |
| 110 | + pass "$test_name" |
| 111 | + else |
| 112 | + fail "$test_name" "$(cat "$posix_file")" "$(cat "$fortsh_file")" |
| 113 | + fi |
| 114 | + |
| 115 | + rm -f "$posix_file" "$fortsh_file" |
| 116 | +} |
| 117 | + |
| 118 | +# Normalize shell error messages by stripping shell name and "line N: " prefix |
| 119 | +# POSIX doesn't mandate error message format, so we normalize for comparison |
| 120 | +normalize_error() { |
| 121 | + echo "$1" | sed -e 's|^[^ ]*bash: |sh: |' -e 's|^[^ ]*fortsh: |sh: |' -e 's/line [0-9]*: //' |
| 122 | +} |
| 123 | + |
| 124 | +# Compare error output, normalizing line number differences |
| 125 | +compare_posix_error() { |
| 126 | + test_name="$1" |
| 127 | + command="$2" |
| 128 | + posix_file="/tmp/posix_gaps_$$_posix" |
| 129 | + fortsh_file="/tmp/posix_gaps_$$_fortsh" |
| 130 | + |
| 131 | + # Run in POSIX shell (sh) |
| 132 | + "$BASH_REF" -c "$command" > "$posix_file" 2>&1 || true |
| 133 | + |
| 134 | + # Run in fortsh |
| 135 | + "$FORTSH_BIN" -c "$command" > "$fortsh_file" 2>&1 || true |
| 136 | + |
| 137 | + # Normalize error messages before comparison |
| 138 | + posix_norm=$(normalize_error "$(cat "$posix_file")") |
| 139 | + fortsh_norm=$(normalize_error "$(cat "$fortsh_file")") |
| 140 | + |
| 141 | + if [ "$posix_norm" = "$fortsh_norm" ]; then |
| 142 | + pass "$test_name" |
| 143 | + else |
| 144 | + fail "$test_name" "$(cat "$posix_file")" "$(cat "$fortsh_file")" |
| 145 | + fi |
| 146 | + |
| 147 | + rm -f "$posix_file" "$fortsh_file" |
| 148 | +} |
| 149 | + |
| 150 | +# Helper function to compare exit codes |
| 151 | +compare_posix_exit_code() { |
| 152 | + test_name="$1" |
| 153 | + command="$2" |
| 154 | + |
| 155 | + "$BASH_REF" -c "$command" > /dev/null 2>&1 |
| 156 | + posix_exit=$? |
| 157 | + |
| 158 | + "$FORTSH_BIN" -c "$command" > /dev/null 2>&1 |
| 159 | + fortsh_exit=$? |
| 160 | + |
| 161 | + if [ "$posix_exit" -eq "$fortsh_exit" ]; then |
| 162 | + pass "$test_name" |
| 163 | + else |
| 164 | + fail "$test_name" "exit=$posix_exit" "exit=$fortsh_exit" |
| 165 | + # Accumulate debug info for CI summary |
| 166 | + DEBUG_INFO="${DEBUG_INFO}DEBUG [$test_name]: cmd='$command'\n" |
| 167 | + DEBUG_INFO="${DEBUG_INFO} bash exit: $posix_exit\n" |
| 168 | + DEBUG_INFO="${DEBUG_INFO} fortsh exit: $fortsh_exit\n" |
| 169 | + fi |
| 170 | +} |
| 171 | + |
| 172 | +# Cleanup |
| 173 | +cleanup() { |
| 174 | + rm -f /tmp/posix_gaps_$$_* 2>/dev/null |
| 175 | + rm -rf /tmp/posix_gaps_test_* 2>/dev/null |
| 176 | +} |
| 177 | +trap cleanup EXIT INT TERM |
| 178 | + |
| 179 | +section "97. PIPELINE EXIT STATUS" |
| 180 | + |
| 181 | +compare_posix_exit_code "pipeline last command status" "true | true | false" |
| 182 | +compare_posix_exit_code "pipeline first fails" "false | true | true" |
| 183 | +compare_posix_exit_code "pipeline middle fails" "true | false | true" |
| 184 | +compare_posix_output "PIPESTATUS concept" "false | true | true; echo \$?" |
| 185 | + |
| 186 | +section "99. PATHNAME EXPANSION EDGE CASES" |
| 187 | + |
| 188 | +mkdir -p /tmp/posix_gaps_test_glob |
| 189 | +touch "/tmp/posix_gaps_test_glob/file1.txt" |
| 190 | +touch "/tmp/posix_gaps_test_glob/file2.txt" |
| 191 | +touch "/tmp/posix_gaps_test_glob/file[3].txt" |
| 192 | +touch "/tmp/posix_gaps_test_glob/-file.txt" |
| 193 | +mkdir -p "/tmp/posix_gaps_test_glob/.hidden" |
| 194 | + |
| 195 | +compare_posix_output "glob bracket literal" "ls /tmp/posix_gaps_test_glob/file[[]3].txt 2>/dev/null | wc -l" |
| 196 | +compare_posix_output "glob dash in bracket" "ls /tmp/posix_gaps_test_glob/[-a-z]file.txt 2>/dev/null | wc -l" |
| 197 | +compare_posix_output "glob no match returns literal" "echo /tmp/posix_gaps_test_glob/*.xyz | grep -c '\\*'" |
| 198 | +compare_posix_output "glob hidden dirs" "ls -d /tmp/posix_gaps_test_glob/.* 2>/dev/null | grep -c hidden" |
| 199 | +compare_posix_output "glob character class digit" "touch /tmp/posix_gaps_test_glob/f1.txt; ls /tmp/posix_gaps_test_glob/f[[:digit:]].txt 2>/dev/null | wc -l" |
| 200 | + |
| 201 | +rm -rf /tmp/posix_gaps_test_glob |
| 202 | + |
| 203 | +section "100. FUNCTION SCOPE AND RECURSION EDGE CASES" |
| 204 | + |
| 205 | +compare_posix_output "function local scope via subshell" "f() { (X=inner; echo \$X); }; X=outer; f; echo \$X" |
| 206 | +compare_posix_output "function positional params" "f() { echo \$1 \$2; }; set -- a b; f x y" |
| 207 | +compare_posix_output "function preserves positional" "f() { echo \$1; }; set -- a b; f x; echo \$1" |
| 208 | +compare_posix_output "nested function calls" "a() { b; }; b() { c; }; c() { echo deep; }; a" |
| 209 | +compare_posix_output "function return in subshell" "f() { (return 5); echo \$?; }; f" |
| 210 | +compare_posix_output "recursive factorial" "fact() { if [ \$1 -le 1 ]; then echo 1; else local r=\$(fact \$((\$1-1)) 2>/dev/null || fact \$((\$1-1))); echo \$((\$1 * r)); fi; }; fact 4" |
| 211 | + |
| 212 | +section "101. EXIT STATUS IN COMPOUND COMMANDS" |
| 213 | + |
| 214 | +compare_posix_exit_code "subshell exit status" "(exit 42); echo \$?" |
| 215 | +compare_posix_exit_code "brace group exit status" "{ exit 42; }; echo \$?" |
| 216 | +compare_posix_exit_code "if statement exit status" "if true; then true; fi; echo \$?" |
| 217 | +compare_posix_exit_code "for loop exit status" "for i in 1; do false; done; echo \$?" |
| 218 | +compare_posix_exit_code "while loop exit status" "while false; do :; done; echo \$?" |
| 219 | +compare_posix_exit_code "case exit status" "case x in x) false;; esac; echo \$?" |
| 220 | + |
| 221 | +section "114. ALIAS EDGE CASES" |
| 222 | + |
| 223 | +compare_posix_output "alias with args" "alias ll='ls -l'; alias ll | grep -c 'ls -l'" |
| 224 | +compare_posix_output "alias recursive prevention" "alias ls='ls -a'; command ls /tmp >/dev/null; echo \$?" |
| 225 | +compare_posix_output "unalias nonexistent" "unalias nonexistent_alias_$$ 2>/dev/null || echo ok" |
| 226 | +compare_posix_output "alias name same as builtin" "alias echo='printf'; unalias echo; command -v echo | grep -c echo" |
| 227 | + |
| 228 | +section "115. TEST COMMAND COMPLEX EXPRESSIONS" |
| 229 | + |
| 230 | +compare_posix_exit_code "test complex AND/OR" "test 5 -gt 3 -a \( 10 -lt 20 -o 1 -eq 2 \)" |
| 231 | +compare_posix_exit_code "test negation precedence" "test ! 5 -gt 10" |
| 232 | +compare_posix_exit_code "test string empty" "test -z ''" |
| 233 | +compare_posix_exit_code "test string nonempty" "test -n 'x'" |
| 234 | +compare_posix_exit_code "test string unary" "test 'nonempty'" |
| 235 | + |
| 236 | +section "118. COMMAND SUBSTITUTION EDGE CASES" |
| 237 | + |
| 238 | +compare_posix_output "command subst nested" "echo \$(echo \$(echo nested))" |
| 239 | +compare_posix_output "command subst with pipes" "echo \$(echo a | cat)" |
| 240 | +compare_posix_output "command subst multiline" "result=\$(echo a; echo b); echo \"\$result\" | wc -l" |
| 241 | +compare_posix_output "command subst empty" "x=\$(true); echo \"|\$x|\"" |
| 242 | +compare_posix_output "backtick vs dollar-paren" "a=\`echo test\`; b=\$(echo test); test \"\$a\" = \"\$b\" && echo same" |
| 243 | + |
| 244 | +section "130. EXPORT EDGE CASES" |
| 245 | + |
| 246 | +compare_posix_output "export without value" "export VAR; sh -c 'echo \${VAR:-unset}'" |
| 247 | +compare_posix_output "export with value" "export VAR=value; sh -c 'echo \$VAR'" |
| 248 | +compare_posix_output "export multiple" "export A=1 B=2; sh -c 'echo \$A \$B'" |
| 249 | +compare_posix_output "export readonly" "readonly X=ro; export X; sh -c 'echo \$X'" |
| 250 | + |
| 251 | +section "131. VARIABLE SCOPE EDGE CASES" |
| 252 | + |
| 253 | +compare_posix_output "var in subshell lost" 'X=1; (X=2); echo $X' |
| 254 | +compare_posix_output "var in brace group kept" 'X=1; { X=2; }; echo $X' |
| 255 | +compare_posix_output "env var in subshell" 'export X=1; (echo $X)' |
| 256 | +compare_posix_output "command prefix assignment" 'X=val sh -c "echo \$X"' |
| 257 | + |
| 258 | +section "132. FUNCTION VARIABLE SCOPE" |
| 259 | + |
| 260 | +compare_posix_output "global in function" 'X=global; f() { echo $X; }; f' |
| 261 | +compare_posix_output "function modifies global" 'X=1; f() { X=2; }; f; echo $X' |
| 262 | +compare_posix_output "local shadows global" 'X=1; f() { local X=2; echo $X; }; f; echo $X' |
| 263 | + |
| 264 | +section "134. COMMAND SUBSTITUTION EDGE CASES" |
| 265 | + |
| 266 | +compare_posix_output "nested cmd sub" 'echo $(echo $(echo deep))' |
| 267 | +compare_posix_output "cmd sub with quotes" 'echo "$(echo "hello world")"' |
| 268 | +compare_posix_output "cmd sub trailing newline" 'x=$(printf "hi\n"); echo "[$x]"' |
| 269 | +compare_posix_output "backtick substitution" 'echo `echo hello`' |
| 270 | + |
| 271 | +section "137. WORD SPLITTING EDGE CASES" |
| 272 | + |
| 273 | +compare_posix_output "IFS colon split" 'IFS=:; x="a:b:c"; set -- $x; echo $#' |
| 274 | +compare_posix_output "IFS multiple chars" 'IFS=":;"; x="a:b;c"; set -- $x; echo $#' |
| 275 | +compare_posix_output "empty IFS no split" 'IFS=""; x="a b c"; set -- $x; echo $#' |
| 276 | +compare_posix_output "default IFS" 'unset IFS; x="a b"; set -- $x; echo $#' |
| 277 | + |
| 278 | +section "138. GLOB EDGE CASES" |
| 279 | + |
| 280 | +compare_posix_output "glob no match" 'echo /nonexistent_dir_xyz_$$/* 2>/dev/null | grep -c nonexistent || echo "0 or pattern"' |
| 281 | +compare_posix_output "set -f disables glob" 'set -f; echo *; set +f' |
| 282 | +compare_posix_output "quoted glob literal" 'echo "*"' |
| 283 | + |
| 284 | +section "139. SIGNAL EDGE CASES" |
| 285 | + |
| 286 | +compare_posix_output "trap list" 'trap "echo x" INT; trap | grep -c INT || echo 0' |
| 287 | +compare_posix_output "trap reset" 'trap "echo x" INT; trap - INT; trap | grep -c INT || echo 0' |
| 288 | +compare_posix_output "trap EXIT runs" 'sh -c "trap echo_exit EXIT; exit 0" 2>/dev/null; echo done' |
| 289 | + |
| 290 | +section "140. MISCELLANEOUS EDGE CASES" |
| 291 | + |
| 292 | +compare_posix_output "empty for list" 'for x in; do echo x; done; echo done' |
| 293 | +compare_posix_output "case no match" 'case x in y) echo y;; esac; echo done' |
| 294 | +compare_posix_output "if false branch" 'if false; then echo yes; else echo no; fi' |
| 295 | +compare_posix_output "elif chain" 'if false; then echo 1; elif false; then echo 2; else echo 3; fi' |
| 296 | +compare_posix_output "nested if" 'if true; then if true; then echo nested; fi; fi' |
| 297 | + |
| 298 | +section "141. ADDITIONAL EDGE CASES" |
| 299 | + |
| 300 | +compare_posix_output "while zero iterations" 'while false; do echo never; done; echo done' |
| 301 | +compare_posix_output "until zero iterations" 'until true; do echo never; done; echo done' |
| 302 | +compare_posix_output "function empty body" 'f() { :; }; f; echo $?' |
| 303 | +compare_posix_output "pipeline single" 'echo test | cat' |
| 304 | +compare_posix_output "assignment no space" 'x=value; echo $x' |
| 305 | +compare_posix_output "comment mid-line" 'echo visible # hidden' |
| 306 | +compare_posix_output "newline continuation" 'echo hel\ |
| 307 | +lo' |
| 308 | +compare_posix_output "mixed operators" 'true && false || echo fallback' |
| 309 | + |
| 310 | +section "142. ADDITIONAL GAPS" |
| 311 | + |
| 312 | +compare_posix_output "expr string length" 'expr length "hello"' |
| 313 | +compare_posix_output "test with and" '[ 1 -eq 1 ] && [ 2 -eq 2 ] && echo both' |
| 314 | +compare_posix_output "test with or" '[ 1 -eq 2 ] || [ 2 -eq 2 ] && echo one' |
| 315 | +compare_posix_output "double negation" '! ! true && echo yes' |
| 316 | +compare_posix_output "triple pipe" 'echo test | cat | cat | cat' |
| 317 | +compare_posix_output "var in quotes" 'X="hello world"; echo "$X"' |
| 318 | +compare_posix_output "arithmetic compare" 'echo $((5 > 3))' |
| 319 | +compare_posix_output "arithmetic ternary sim" '[ 5 -gt 3 ] && echo big || echo small' |
| 320 | +compare_posix_output "for with seq" 'for i in 1 2 3 4 5; do echo $i; done | wc -l' |
| 321 | +compare_posix_output "while decrement" 'i=5; while [ $i -gt 0 ]; do i=$((i-1)); done; echo $i' |
| 322 | +compare_posix_output "case star pattern" 'x=anything; case $x in *) echo star;; esac' |
| 323 | +compare_posix_output "func recursion base" 'f() { [ $1 -le 0 ] && echo done || f $(($1-1)); }; f 3' |
| 324 | +compare_posix_output "heredoc simple" 'cat <<END |
| 325 | +test |
| 326 | +END' |
| 327 | +compare_posix_output "redirect both" 'echo out; echo err >&2' |
| 328 | +compare_posix_output "subshell cd" '(cd /tmp; pwd)' |
| 329 | + |
| 330 | +section "151. FUNCTION PATTERNS" |
| 331 | + |
| 332 | +compare_posix_output "func define call" 'f() { echo hello; }; f' |
| 333 | +compare_posix_output "func with args" 'f() { echo $1 $2; }; f a b' |
| 334 | +compare_posix_output "func return code" 'f() { return 0; }; f; echo $?' |
| 335 | +compare_posix_output "func return 1" 'f() { return 1; }; f; echo $?' |
| 336 | +compare_posix_output "func with local" 'x=outer; f() { x=inner; }; f; echo $x' |
| 337 | +compare_posix_output "func recursive" 'f() { [ $1 -eq 0 ] && echo done || f $(($1-1)); }; f 2' |
| 338 | +compare_posix_output "func in pipeline" 'f() { echo test; }; f | cat' |
| 339 | +compare_posix_output "func multi stmt" 'f() { echo a; echo b; }; f | wc -l' |
| 340 | +compare_posix_output "func empty body" 'f() { :; }; f; echo ok' |
| 341 | +compare_posix_output "func all args" 'f() { echo $@; }; f a b c' |
| 342 | + |
| 343 | +section "152. REDIRECTION PATTERNS" |
| 344 | + |
| 345 | +compare_posix_output "redir output" 'echo test > /tmp/test_$$; cat /tmp/test_$$; rm /tmp/test_$$' |
| 346 | +compare_posix_output "redir append" 'echo a > /tmp/test_$$; echo b >> /tmp/test_$$; wc -l < /tmp/test_$$; rm /tmp/test_$$' |
| 347 | +compare_posix_output "redir input" 'echo test > /tmp/test_$$; cat < /tmp/test_$$; rm /tmp/test_$$' |
| 348 | +compare_posix_output "redir stderr" 'echo err >&2 2>/dev/null; echo ok' |
| 349 | +compare_posix_output "redir both devnull" 'echo out; echo err >&2 2>/dev/null' |
| 350 | +compare_posix_output "redir fd dup" 'echo test 2>&1 | cat' |
| 351 | +compare_posix_output "redir here string alt" 'echo test | cat' |
| 352 | +compare_posix_output "redir noclobber safe" 'echo ok' |
| 353 | +compare_posix_output "multiple redir" 'echo a; echo b; echo c' |
| 354 | +compare_posix_output "redir in subshell" '(echo test > /tmp/test_$$); cat /tmp/test_$$ 2>/dev/null; rm /tmp/test_$$ 2>/dev/null; echo done' |
| 355 | + |
| 356 | +section "153. SPECIAL PARAMETERS" |
| 357 | + |
| 358 | +compare_posix_output "dollar question" 'true; echo $?' |
| 359 | +compare_posix_output "dollar question fail" 'false; echo $?' |
| 360 | +compare_posix_output "dollar hyphen" 'echo $- | grep -c "."' |
| 361 | +compare_posix_output "dollar dollar" 'echo $$ | grep -c "[0-9]"' |
| 362 | +compare_posix_output "dollar zero" 'echo ${0:-shell} | grep -c "."' |
| 363 | +compare_posix_output "positional one" 'set -- a b c; echo $1' |
| 364 | +compare_posix_output "positional two" 'set -- a b c; echo $2' |
| 365 | +compare_posix_output "positional three" 'set -- a b c; echo $3' |
| 366 | +compare_posix_output "dollar at" 'set -- a b c; echo "$@"' |
| 367 | +compare_posix_output "dollar star" 'set -- a b c; echo "$*"' |
| 368 | + |
| 369 | +section "154. TEST BUILTIN COMPREHENSIVE" |
| 370 | + |
| 371 | +compare_posix_output "test eq" '[ 1 -eq 1 ]; echo $?' |
| 372 | +compare_posix_output "test ne" '[ 1 -ne 2 ]; echo $?' |
| 373 | +compare_posix_output "test lt" '[ 1 -lt 2 ]; echo $?' |
| 374 | +compare_posix_output "test gt" '[ 2 -gt 1 ]; echo $?' |
| 375 | +compare_posix_output "test le" '[ 1 -le 1 ]; echo $?' |
| 376 | +compare_posix_output "test ge" '[ 1 -ge 1 ]; echo $?' |
| 377 | +compare_posix_output "test str eq" '[ "a" = "a" ]; echo $?' |
| 378 | +compare_posix_output "test str ne" '[ "a" != "b" ]; echo $?' |
| 379 | +compare_posix_output "test z" '[ -z "" ]; echo $?' |
| 380 | +compare_posix_output "test n" '[ -n "x" ]; echo $?' |
| 381 | + |
| 382 | +section "155. FILE TEST OPERATIONS" |
| 383 | + |
| 384 | +compare_posix_output "test f file" '[ -f /etc/passwd ] && echo yes || echo no' |
| 385 | +compare_posix_output "test d dir" '[ -d /tmp ] && echo yes || echo no' |
| 386 | +compare_posix_output "test e exists" '[ -e /tmp ] && echo yes || echo no' |
| 387 | +compare_posix_output "test r read" '[ -r /etc/passwd ] && echo yes || echo no' |
| 388 | +compare_posix_output "test w write" '[ -w /tmp ] && echo yes || echo no' |
| 389 | +compare_posix_output "test x exec" '[ -x /bin/sh ] && echo yes || echo no' |
| 390 | +compare_posix_output "test s size" '[ -s /etc/passwd ] && echo yes || echo no' |
| 391 | +compare_posix_output "test not exist" '[ -e /nonexistent_xyz ] && echo yes || echo no' |
| 392 | +compare_posix_output "test not dir" '[ -d /etc/passwd ] && echo yes || echo no' |
| 393 | +compare_posix_output "test not file" '[ -f /tmp ] && echo yes || echo no' |
| 394 | + |
| 395 | +section "156. LOGICAL COMBINATIONS" |
| 396 | + |
| 397 | +compare_posix_output "and true true" 'true && true; echo $?' |
| 398 | +compare_posix_output "and true false" 'true && false; echo $?' |
| 399 | +compare_posix_output "and false true" 'false && true; echo $?' |
| 400 | +compare_posix_output "and false false" 'false && false; echo $?' |
| 401 | +compare_posix_output "or true true" 'true || true; echo $?' |
| 402 | +compare_posix_output "or true false" 'true || false; echo $?' |
| 403 | +compare_posix_output "or false true" 'false || true; echo $?' |
| 404 | +compare_posix_output "or false false" 'false || false; echo $?' |
| 405 | +compare_posix_output "not true" '! true; echo $?' |
| 406 | +compare_posix_output "not false" '! false; echo $?' |
| 407 | + |
| 408 | +section "157. SUBSHELL AND GROUPING" |
| 409 | + |
| 410 | +compare_posix_output "subshell basic" '(echo sub)' |
| 411 | +compare_posix_output "subshell var scope" 'x=outer; (x=inner); echo $x' |
| 412 | +compare_posix_output "subshell exit" '(exit 5); echo $?' |
| 413 | +compare_posix_output "subshell cd scope" '(cd /tmp); pwd | grep -v "/tmp" | head -1' |
| 414 | +compare_posix_output "brace group" '{ echo brace; }' |
| 415 | +compare_posix_output "brace group var" 'x=outer; { x=inner; }; echo $x' |
| 416 | +compare_posix_output "brace group list" '{ echo a; echo b; } | wc -l' |
| 417 | +compare_posix_output "subshell nested" '(echo $(echo nested))' |
| 418 | +compare_posix_output "mixed grouping" '(echo sub); { echo brace; }' |
| 419 | +compare_posix_output "subshell pipeline" '(echo test) | cat' |
| 420 | + |
| 421 | +section "158. HEREDOC PATTERNS" |
| 422 | + |
| 423 | +compare_posix_output "heredoc basic" 'cat <<EOF |
| 424 | +test |
| 425 | +EOF' |
| 426 | +compare_posix_output "heredoc multi" 'cat <<EOF |
| 427 | +line1 |
| 428 | +line2 |
| 429 | +EOF' |
| 430 | +compare_posix_output "heredoc expand" 'X=val; cat <<EOF |
| 431 | +$X |
| 432 | +EOF' |
| 433 | +compare_posix_output "heredoc quoted" "cat <<'EOF' |
| 434 | +\$X |
| 435 | +EOF" |
| 436 | +compare_posix_output "heredoc tab" 'cat <<-EOF |
| 437 | + indented |
| 438 | +EOF' |
| 439 | +compare_posix_output "heredoc empty" 'cat <<EOF |
| 440 | +EOF |
| 441 | +echo done' |
| 442 | +compare_posix_output "heredoc special" 'cat <<EOF |
| 443 | +* $ " '"'"' |
| 444 | +EOF' |
| 445 | + |
| 446 | +section "159. QUOTING EDGE CASES" |
| 447 | + |
| 448 | +compare_posix_output "double quote var" 'x=val; echo "$x"' |
| 449 | +compare_posix_output "single quote literal" "echo 'literal \$x'" |
| 450 | +compare_posix_output "escape in double" 'echo "a\\b"' |
| 451 | +compare_posix_output "dollar literal" 'echo "\$"' |
| 452 | +compare_posix_output "backtick literal" 'echo "\`"' |
| 453 | +compare_posix_output "newline in quote" 'echo "line1 |
| 454 | +line2"' |
| 455 | +compare_posix_output "tab in quote" 'echo "a b"' |
| 456 | +compare_posix_output "space preservation" 'echo "a b"' |
| 457 | +compare_posix_output "empty string" 'echo ""' |
| 458 | +compare_posix_output "adjacent quotes" 'echo "a""b"' |
| 459 | + |
| 460 | +section "160. EXPANSION ORDER" |
| 461 | + |
| 462 | +compare_posix_output "tilde first" 'echo ~ | grep -c "/"' |
| 463 | +compare_posix_output "param before cmd" 'x=echo; $x hello' |
| 464 | +compare_posix_output "arith in var" 'x=$((1+1)); echo $x' |
| 465 | +compare_posix_output "cmd in arith" 'echo $(($(echo 5) + 1))' |
| 466 | +compare_posix_output "var in glob" 'x="*"; echo "$x"' |
| 467 | +compare_posix_output "split then glob" 'IFS=" "; x="a b"; for i in $x; do echo $i; done | wc -l' |
| 468 | +compare_posix_output "quote prevents split" 'x="a b c"; set -- "$x"; echo $#' |
| 469 | +compare_posix_output "unquote allows split" 'x="a b c"; set -- $x; echo $#' |
| 470 | +compare_posix_output "nested expansion" 'x=y; y=val; eval echo \$$x' |
| 471 | +compare_posix_output "complex chain" 'x=$(echo $((1+2))); echo $x' |
| 472 | + |
| 473 | +section "161. BUILTIN COMMAND VARIATIONS" |
| 474 | + |
| 475 | +compare_posix_output "echo no newline" 'echo -n test; echo done' |
| 476 | +compare_posix_output "echo escape e" 'echo -e "a\tb"' |
| 477 | +compare_posix_output "printf string" 'printf "%s\n" hello' |
| 478 | +compare_posix_output "printf number" 'printf "%d\n" 42' |
| 479 | +compare_posix_output "printf hex" 'printf "%x\n" 255' |
| 480 | +compare_posix_output "printf octal" 'printf "%o\n" 64' |
| 481 | +compare_posix_output "printf width" 'printf "%5d\n" 42' |
| 482 | +compare_posix_output "printf zero pad" 'printf "%05d\n" 42' |
| 483 | +compare_posix_output "printf left align" 'printf "%-5d|\n" 42' |
| 484 | +compare_posix_output "printf multiple" 'printf "%s %s\n" a b' |
| 485 | + |
| 486 | +section "162. PRINTF VARIATIONS" |
| 487 | + |
| 488 | +compare_posix_output "printf basic" 'printf "hello\n"' |
| 489 | +compare_posix_output "printf no newline" 'printf "test"; echo done' |
| 490 | +compare_posix_output "printf format s" 'printf "%s\n" hello' |
| 491 | +compare_posix_output "printf format d" 'printf "%d\n" 42' |
| 492 | +compare_posix_output "printf multi arg" 'printf "%s %s\n" a b' |
| 493 | +compare_posix_output "printf width" 'printf "%5s\n" ab' |
| 494 | +compare_posix_output "printf escape" 'printf "a\tb\n"' |
| 495 | +compare_posix_output "printf percent" 'printf "%%\n"' |
| 496 | + |
| 497 | +section "163. SET BUILTIN VARIATIONS" |
| 498 | + |
| 499 | +compare_posix_output "set positional" 'set -- a b c; echo $1 $2 $3' |
| 500 | +compare_posix_output "set clear" 'set -- a b; set --; echo ${1:-none}' |
| 501 | +compare_posix_output "set e exit" 'set -e; true; echo ok' |
| 502 | +compare_posix_output "set u unset" 'set +u; echo ${undefined_var:-default}' |
| 503 | +compare_posix_output "set f noglob" 'set -f; echo *; set +f' |
| 504 | +compare_posix_output "set minus" 'echo $-' |
| 505 | +compare_posix_output "set show" 'X=1; set | grep -c "^X=" || echo 0' |
| 506 | +compare_posix_output "shift once" 'set -- a b c; shift; echo $1' |
| 507 | +compare_posix_output "shift twice" 'set -- a b c; shift 2; echo $1' |
| 508 | +compare_posix_output "shift all" 'set -- a b; shift 2; echo ${1:-empty}' |
| 509 | + |
| 510 | +section "164. EXPORT AND ENV" |
| 511 | + |
| 512 | +compare_posix_output "export simple" 'export X=val; echo $X' |
| 513 | +compare_posix_output "export existing" 'X=val; export X; echo $X' |
| 514 | +compare_posix_output "export list" 'export X=1 Y=2; echo $X $Y' |
| 515 | +compare_posix_output "export subshell" 'export X=val; (echo $X)' |
| 516 | +compare_posix_output "env prefix" 'X=override sh -c "echo \$X"' |
| 517 | +compare_posix_output "env clear" 'X=val; unset X; echo ${X:-unset}' |
| 518 | +compare_posix_output "readonly var" 'readonly X=const; echo $X' |
| 519 | +compare_posix_output "unset normal" 'X=val; unset X; echo ${X:-gone}' |
| 520 | + |
| 521 | +section "165. TRAP VARIATIONS" |
| 522 | + |
| 523 | +# Note: Initial trap state may vary by environment - check both produce some output |
| 524 | +compare_posix_exit_code "trap list empty" 'trap >/dev/null 2>&1' |
| 525 | +compare_posix_output "trap set list" 'trap "echo x" INT; trap | grep -c INT || echo 0' |
| 526 | +compare_posix_output "trap reset" 'trap "echo x" INT; trap - INT; trap | grep -c INT || echo 0' |
| 527 | +compare_posix_output "trap exit" 'sh -c "trap \"echo exit\" EXIT" 2>/dev/null || echo done' |
| 528 | +compare_posix_output "trap ignore" 'trap "" INT; trap | grep -c INT || echo 0' |
| 529 | +compare_posix_output "trap in func" 'f() { trap "echo trap" EXIT; }; f 2>/dev/null; echo done' |
| 530 | + |
| 531 | +section "166. EVAL AND EXEC" |
| 532 | + |
| 533 | +compare_posix_output "eval simple" 'eval echo hello' |
| 534 | +compare_posix_output "eval var" 'X=world; eval echo hello $X' |
| 535 | +compare_posix_output "eval cmd" 'eval "echo test"' |
| 536 | +compare_posix_output "eval multi" 'eval "echo a; echo b"' |
| 537 | +compare_posix_output "eval indirect" 'X=Y; Y=val; eval echo \$$X' |
| 538 | +compare_posix_output "exec redirect" 'exec 3>&1; echo test >&3' |
| 539 | +compare_posix_output "exec close" 'exec 3>&1; exec 3>&-; echo ok' |
| 540 | + |
| 541 | +section "167. DOT AND SOURCE" |
| 542 | + |
| 543 | +compare_posix_output "dot inline" 'echo "X=sourced" > /tmp/src_$$; . /tmp/src_$$; echo $X; rm /tmp/src_$$' |
| 544 | +compare_posix_output "dot func" 'echo "f() { echo func; }" > /tmp/src_$$; . /tmp/src_$$; f; rm /tmp/src_$$' |
| 545 | +compare_posix_output "dot var persist" 'echo "Y=persist" > /tmp/src_$$; . /tmp/src_$$; echo $Y; rm /tmp/src_$$' |
| 546 | + |
| 547 | +section "168. COMMAND AND TYPE" |
| 548 | + |
| 549 | +compare_posix_output "command echo" 'command echo hello' |
| 550 | +compare_posix_output "command builtin" 'command true; echo $?' |
| 551 | +compare_posix_output "command v" 'command -v echo | grep -c echo' |
| 552 | +compare_posix_output "type builtin" 'type echo 2>/dev/null | grep -c echo || echo 1' |
| 553 | +compare_posix_output "type not found" 'type nonexistent_cmd_xyz 2>&1 | grep -c "not found" || echo 0' |
| 554 | + |
| 555 | +section "169. GETOPTS VARIATIONS" |
| 556 | + |
| 557 | +compare_posix_output "getopts simple" 'set -- -a; getopts a opt; echo $opt' |
| 558 | +compare_posix_output "getopts arg" 'set -- -a val; getopts a: opt; echo $opt $OPTARG' |
| 559 | +compare_posix_output "getopts multi" 'set -- -a -b; getopts ab opt && echo $opt' |
| 560 | +compare_posix_output "getopts unknown" 'set -- -x; getopts a opt 2>/dev/null; echo ${opt:-?}' |
| 561 | +compare_posix_output "getopts optind" 'set -- -a arg; getopts a opt; echo $OPTIND' |
| 562 | + |
| 563 | +section "170. COLON AND TRUE/FALSE" |
| 564 | + |
| 565 | +compare_posix_output "colon noop" ':; echo $?' |
| 566 | +compare_posix_output "colon with args" ': arg1 arg2; echo $?' |
| 567 | +compare_posix_output "true exit" 'true; echo $?' |
| 568 | +compare_posix_output "false exit" 'false; echo $?' |
| 569 | +compare_posix_output "colon in if" 'if :; then echo yes; fi' |
| 570 | +compare_posix_output "colon in while" 'n=0; while :; do n=$((n+1)); [ $n -ge 3 ] && break; done; echo $n' |
| 571 | + |
| 572 | +section "171. PWD AND CD" |
| 573 | + |
| 574 | +compare_posix_output "pwd basic" 'pwd | grep -c "/"' |
| 575 | +compare_posix_output "cd tmp" 'cd /tmp && pwd' |
| 576 | +compare_posix_output "cd home" 'cd ~ && pwd | grep -c "/"' |
| 577 | +compare_posix_output "cd dash" 'cd /tmp; cd /; cd -' |
| 578 | +compare_posix_output "cd dotdot" 'cd /tmp; cd ..; pwd' |
| 579 | +compare_posix_output "cd subshell" '(cd /tmp; pwd); pwd | grep -v "/tmp" | wc -l' |
| 580 | +compare_posix_output "OLDPWD" 'cd /tmp; cd /; echo $OLDPWD' |
| 581 | + |
| 582 | +section "172. UMASK VARIATIONS" |
| 583 | + |
| 584 | +compare_posix_output "umask get" 'umask | grep -c "[0-7]"' |
| 585 | +compare_posix_output "umask symbolic" 'umask -S | grep -c "u="' |
| 586 | + |
| 587 | +section "173. TIMES BUILTIN" |
| 588 | + |
| 589 | +compare_posix_output "times format" 'times 2>&1 | head -1 | grep -c "[0-9]" || echo 0' |
| 590 | + |
| 591 | +section "174. HASH BUILTIN" |
| 592 | + |
| 593 | +compare_posix_output "hash list" 'hash 2>/dev/null; echo $?' |
| 594 | +compare_posix_output "hash r clear" 'hash -r 2>/dev/null; echo $?' |
| 595 | + |
| 596 | +section "175. ALIAS VARIATIONS" |
| 597 | + |
| 598 | +compare_posix_output "alias set" 'alias x=echo 2>/dev/null; echo ok' |
| 599 | +compare_posix_output "alias list" 'alias 2>/dev/null; echo $?' |
| 600 | +compare_posix_output "unalias" 'alias x=echo 2>/dev/null; unalias x 2>/dev/null; echo $?' |
| 601 | + |
| 602 | +section "176. ADDITIONAL BUILTINS" |
| 603 | + |
| 604 | +compare_posix_output "test bracket" '[ 1 -eq 1 ]; echo $?' |
| 605 | +compare_posix_output "test not" '[ ! 1 -eq 2 ]; echo $?' |
| 606 | +compare_posix_output "test and" '[ 1 -eq 1 ] && [ 2 -eq 2 ]; echo $?' |
| 607 | + |
| 608 | +section "177. MORE CONTROL FLOW" |
| 609 | + |
| 610 | +compare_posix_output "if compound" 'if [ 1 -eq 1 ] && [ 2 -eq 2 ]; then echo yes; fi' |
| 611 | +compare_posix_output "while compound" 'n=2; while [ $n -gt 0 ] && true; do n=$((n-1)); done; echo $n' |
| 612 | + |
| 613 | +section "178. ULIMIT VARIATIONS" |
| 614 | + |
| 615 | +compare_posix_output "ulimit soft" 'ulimit -S -n 2>/dev/null | grep -c "[0-9]" || echo 1' |
| 616 | +compare_posix_output "ulimit hard" 'ulimit -H -n 2>/dev/null | grep -c "[0-9]" || echo 1' |
| 617 | +# Note: fortsh implements fewer limit types than bash - check both produce output |
| 618 | +compare_posix_exit_code "ulimit all" 'test $(ulimit -a 2>/dev/null | wc -l) -ge 5' |
| 619 | + |
| 620 | +section "179. SPECIAL EXPANSIONS" |
| 621 | + |
| 622 | +compare_posix_output "length special" 'set -- a b c; echo ${#@}' |
| 623 | +compare_posix_output "length star" 'set -- a b c; echo ${#*}' |
| 624 | +compare_posix_output "at in quotes" 'set -- a b c; for x in "$@"; do echo $x; done | wc -l' |
| 625 | +compare_posix_output "star in quotes" 'set -- a b c; echo "$*" | wc -w' |
| 626 | +compare_posix_output "hash positional" 'set -- a b c d e; echo $#' |
| 627 | +compare_posix_output "shift and hash" 'set -- a b c; shift; echo $#' |
| 628 | +compare_posix_output "at unquoted" 'set -- a b c; for x in $@; do echo $x; done | wc -l' |
| 629 | +compare_posix_output "star unquoted" 'set -- a b c; for x in $*; do echo $x; done | wc -l' |
| 630 | + |
| 631 | +section "180. COMPLEX PATTERNS" |
| 632 | + |
| 633 | +compare_posix_output "nested cmd sub" 'echo $(echo $(echo $(echo deep)))' |
| 634 | +compare_posix_output "nested arith" 'echo $(( $(( $(( 1+1 )) + 1 )) + 1 ))' |
| 635 | +compare_posix_output "mixed nesting" 'echo $(( $(echo 5) + $(echo 3) ))' |
| 636 | +compare_posix_output "pipeline chain" 'echo test | cat | cat | cat | cat' |
| 637 | +compare_posix_output "long and chain" 'true && true && true && true && echo yes' |
| 638 | +compare_posix_output "long or chain" 'false || false || false || true && echo yes' |
| 639 | +compare_posix_output "mixed logic" 'true && false || true && echo yes' |
| 640 | +# Note: (((( ))) is arithmetic syntax. Compare exit codes. |
| 641 | +compare_posix_exit_code "quad paren arithmetic" '((((echo deep))))' |
| 642 | +compare_posix_output "nested brace" '{ { { echo deep; }; }; }' |
| 643 | +compare_posix_output "func chain" 'f() { echo $1; }; g() { f hello; }; g' |
| 644 | + |
| 645 | +section "181. WORD BOUNDARY CASES" |
| 646 | + |
| 647 | +compare_posix_output "empty word" 'echo "" | cat' |
| 648 | +compare_posix_output "space word" 'echo " " | cat' |
| 649 | +compare_posix_output "tab word" 'echo " " | cat' |
| 650 | +compare_posix_output "newline word" 'echo " |
| 651 | +" | wc -l' |
| 652 | +compare_posix_output "mixed whitespace" 'echo " " | cat' |
| 653 | +compare_posix_output "leading space" 'echo " test" | cat' |
| 654 | +compare_posix_output "trailing space" 'echo "test " | cat' |
| 655 | +compare_posix_output "multiple spaces" 'echo "a b" | cat' |
| 656 | + |
| 657 | +section "182. NUMERIC EDGE CASES" |
| 658 | + |
| 659 | +compare_posix_output "zero" 'echo $((0))' |
| 660 | +compare_posix_output "negative one" 'echo $((-1))' |
| 661 | +compare_posix_output "large number" 'echo $((999999))' |
| 662 | +compare_posix_output "add negative" 'echo $((5 + -3))' |
| 663 | +compare_posix_output "sub negative" 'echo $((5 - -3))' |
| 664 | +compare_posix_output "mult negative" 'echo $((5 * -3))' |
| 665 | +compare_posix_output "div negative" 'echo $((-15 / 3))' |
| 666 | +compare_posix_output "mod negative" 'echo $((-7 % 3))' |
| 667 | +compare_posix_output "compare negative" 'echo $((-1 < 0))' |
| 668 | +compare_posix_output "zero compare" 'echo $((0 == 0))' |
| 669 | + |
| 670 | +section "183. STRING BOUNDARY CASES" |
| 671 | + |
| 672 | +compare_posix_output "empty length" 'x=""; echo ${#x}' |
| 673 | +compare_posix_output "single char" 'x="a"; echo ${#x}' |
| 674 | +compare_posix_output "special chars" 'x="!@#"; echo ${#x}' |
| 675 | +compare_posix_output "spaces length" 'x="a b c"; echo ${#x}' |
| 676 | +compare_posix_output "suffix on empty" 'x=""; echo ${x%.txt}' |
| 677 | +compare_posix_output "prefix on empty" 'x=""; echo ${x#pre}' |
| 678 | +compare_posix_output "default on set" 'x="val"; echo ${x:-def}' |
| 679 | +compare_posix_output "alt on unset" 'unset x; echo ${x:+alt}' |
| 680 | +compare_posix_output "alt on set" 'x="val"; echo ${x:+alt}' |
| 681 | +compare_posix_output "assign on set" 'x="val"; echo ${x:=new}; echo $x' |
| 682 | + |
| 683 | +section "184. GLOB BOUNDARY CASES" |
| 684 | + |
| 685 | +compare_posix_output "star only" 'echo "*"' |
| 686 | +compare_posix_output "question only" 'echo "?"' |
| 687 | +compare_posix_output "bracket only" 'echo "[a]"' |
| 688 | +compare_posix_output "glob in quotes" 'echo "*.txt"' |
| 689 | +compare_posix_output "glob escaped" 'echo \*' |
| 690 | +compare_posix_output "noglob star" 'set -f; echo *; set +f' |
| 691 | +compare_posix_output "noglob question" 'set -f; echo ?; set +f' |
| 692 | +compare_posix_output "noglob bracket" 'set -f; echo [a]; set +f' |
| 693 | + |
| 694 | +section "185. REDIRECTION BOUNDARY CASES" |
| 695 | + |
| 696 | +compare_posix_output "redir empty file" 'echo -n > /tmp/empty_$$; wc -c < /tmp/empty_$$; rm /tmp/empty_$$' |
| 697 | +compare_posix_output "append to new" 'rm -f /tmp/new_$$; echo test >> /tmp/new_$$; cat /tmp/new_$$; rm /tmp/new_$$' |
| 698 | +compare_posix_output "input from empty" 'echo -n > /tmp/empty_$$; cat < /tmp/empty_$$; echo done; rm /tmp/empty_$$' |
| 699 | +compare_posix_output "stderr only" 'echo err >&2 2>&1 | cat' |
| 700 | +compare_posix_output "stdout to null" 'echo test > /dev/null; echo $?' |
| 701 | +compare_posix_output "stderr to null" 'echo err >&2 2>/dev/null; echo done' |
| 702 | + |
| 703 | +section "186. PIPELINE BOUNDARY CASES" |
| 704 | + |
| 705 | +compare_posix_output "empty input pipe" 'echo -n | cat' |
| 706 | +compare_posix_output "single char pipe" 'echo a | cat' |
| 707 | +compare_posix_output "large pipe" 'seq 1 100 | wc -l' |
| 708 | +compare_posix_output "pipe to head 1" 'seq 1 10 | head -1' |
| 709 | +compare_posix_output "pipe to tail 1" 'seq 1 10 | tail -1' |
| 710 | +compare_posix_output "multi filter" 'seq 1 10 | head -5 | tail -1' |
| 711 | + |
| 712 | +section "187. LOOP BOUNDARY CASES" |
| 713 | + |
| 714 | +compare_posix_output "for single item" 'for i in x; do echo $i; done' |
| 715 | +compare_posix_output "for no items" 'for i in; do echo $i; done; echo done' |
| 716 | +compare_posix_output "while never" 'while false; do echo no; done; echo done' |
| 717 | +compare_posix_output "until immediate" 'until true; do echo no; done; echo done' |
| 718 | +compare_posix_output "break immediate" 'for i in 1 2 3; do break; echo $i; done; echo done' |
| 719 | +compare_posix_output "continue all" 'for i in 1 2 3; do continue; echo $i; done; echo done' |
| 720 | +compare_posix_output "nested break" 'for i in 1 2; do for j in a b; do break; done; echo $i; done' |
| 721 | +compare_posix_output "break 2" 'for i in 1 2; do for j in a b; do break 2; done; done; echo done' |
| 722 | + |
| 723 | +section "188. FUNCTION BOUNDARY CASES" |
| 724 | + |
| 725 | +compare_posix_output "func no args" 'f() { echo ${1:-none}; }; f' |
| 726 | +compare_posix_output "func many args" 'f() { echo $#; }; f a b c d e f g h i j' |
| 727 | +compare_posix_output "func return max" 'f() { return 255; }; f; echo $?' |
| 728 | +compare_posix_output "func return zero" 'f() { return 0; }; f; echo $?' |
| 729 | +compare_posix_output "func empty" 'f() { :; }; f; echo $?' |
| 730 | +compare_posix_output "func redefine" 'f() { echo one; }; f() { echo two; }; f' |
| 731 | +compare_posix_output "func recursive depth" 'f() { [ $1 -eq 0 ] && echo done || f $(($1-1)); }; f 5' |
| 732 | + |
| 733 | +section "189. CASE BOUNDARY CASES" |
| 734 | + |
| 735 | +compare_posix_output "case empty string" 'case "" in "") echo empty;; esac' |
| 736 | +compare_posix_output "case single char" 'case "x" in x) echo x;; esac' |
| 737 | +compare_posix_output "case no patterns" 'case x in esac; echo done' |
| 738 | +compare_posix_output "case all patterns" 'case x in a|b|c|x|y) echo match;; esac' |
| 739 | +compare_posix_output "case star first" 'case x in *) echo star;; x) echo x;; esac' |
| 740 | +compare_posix_output "case question" 'case ab in ??) echo two;; esac' |
| 741 | +compare_posix_output "case bracket range" 'case m in [a-z]) echo lower;; esac' |
| 742 | +compare_posix_output "case bracket neg" 'case 5 in [!a-z]) echo notlower;; esac' |
| 743 | + |
| 744 | +section "190. SUBSHELL BOUNDARY CASES" |
| 745 | + |
| 746 | +compare_posix_output "subshell empty" '(); echo $?' |
| 747 | +compare_posix_output "subshell exit 0" '(exit 0); echo $?' |
| 748 | +compare_posix_output "subshell exit 1" '(exit 1); echo $?' |
| 749 | +compare_posix_output "subshell var" '(X=inner; echo $X)' |
| 750 | +compare_posix_output "subshell no leak" 'X=outer; (X=inner); echo $X' |
| 751 | +# Note: (( )) is arithmetic syntax. Compare exit codes. |
| 752 | +compare_posix_exit_code "subshell nested arithmetic" '((echo deep))' |
| 753 | +compare_posix_output "subshell pipe" '(echo test) | (cat)' |
| 754 | +compare_posix_output "subshell output" '(echo sub; echo shell) | wc -l' |
| 755 | + |
| 756 | +section "191. BRACE GROUP BOUNDARY CASES" |
| 757 | + |
| 758 | +compare_posix_output "brace simple" '{ echo test; }' |
| 759 | +compare_posix_output "brace multi" '{ echo a; echo b; }' |
| 760 | +compare_posix_output "brace var" '{ X=val; }; echo $X' |
| 761 | +compare_posix_output "brace exit" '{ exit 0; }; echo $?' |
| 762 | +compare_posix_output "brace pipe" '{ echo test; } | cat' |
| 763 | +compare_posix_output "brace nested" '{ { echo deep; }; }' |
| 764 | +compare_posix_output "brace and sub" '{ (echo sub); echo brace; }' |
| 765 | +compare_posix_output "brace redirect" '{ echo test; } > /tmp/br_$$; cat /tmp/br_$$; rm /tmp/br_$$' |
| 766 | + |
| 767 | +section "192. COMMENT VARIATIONS" |
| 768 | + |
| 769 | +compare_posix_output "comment end" 'echo test # comment' |
| 770 | +compare_posix_output "comment only" '# just a comment |
| 771 | +echo ok' |
| 772 | +compare_posix_output "comment in quotes" 'echo "# not comment"' |
| 773 | +compare_posix_output "hash in var" 'X="#value"; echo $X' |
| 774 | +compare_posix_output "comment after semi" 'echo a; # comment |
| 775 | +echo b' |
| 776 | + |
| 777 | +section "193. LINE CONTINUATION" |
| 778 | + |
| 779 | +compare_posix_output "backslash newline" 'echo hel\ |
| 780 | +lo' |
| 781 | +compare_posix_output "continuation in cmd" 'ec\ |
| 782 | +ho test' |
| 783 | +compare_posix_output "continuation in arg" 'echo "hel\ |
| 784 | +lo"' |
| 785 | +compare_posix_output "multi continuation" 'echo a\ |
| 786 | +b\ |
| 787 | +c' |
| 788 | + |
| 789 | +section "194. SEMICOLON VARIATIONS" |
| 790 | + |
| 791 | +compare_posix_output "semi two cmds" 'echo a; echo b' |
| 792 | +compare_posix_output "semi three cmds" 'echo a; echo b; echo c' |
| 793 | +compare_posix_output "semi with space" 'echo a ; echo b' |
| 794 | +compare_posix_output "semi end of line" 'echo test;' |
| 795 | +compare_posix_output "semi in quotes" 'echo "a;b"' |
| 796 | +compare_posix_output "semi empty" '; echo ok' |
| 797 | + |
| 798 | +section "195. NEWLINE AS SEPARATOR" |
| 799 | + |
| 800 | +compare_posix_output "newline sep" 'echo a |
| 801 | +echo b' |
| 802 | +compare_posix_output "newline in if" 'if true |
| 803 | +then |
| 804 | +echo yes |
| 805 | +fi' |
| 806 | +compare_posix_output "newline in for" 'for i in 1 2 |
| 807 | +do |
| 808 | +echo $i |
| 809 | +done | wc -l' |
| 810 | +compare_posix_output "newline in case" 'case x in |
| 811 | +x) echo yes;; |
| 812 | +esac' |
| 813 | + |
| 814 | +section "196. AMPERSAND VARIATIONS" |
| 815 | + |
| 816 | +compare_posix_output "and simple" 'true && echo yes' |
| 817 | +compare_posix_output "and fail" 'false && echo no; echo done' |
| 818 | +compare_posix_output "and chain" 'true && true && echo yes' |
| 819 | +compare_posix_output "and or mix" 'true && true || echo no' |
| 820 | +compare_posix_output "or and mix" 'false || true && echo yes' |
| 821 | +compare_posix_output "triple and" 'true && true && true && echo yes' |
| 822 | + |
| 823 | +section "197. PIPE VARIATIONS" |
| 824 | + |
| 825 | +compare_posix_output "or simple" 'false || echo yes' |
| 826 | +compare_posix_output "or pass" 'true || echo no; echo done' |
| 827 | +compare_posix_output "or chain" 'false || false || echo yes' |
| 828 | +compare_posix_output "pipe simple" 'echo test | cat' |
| 829 | +compare_posix_output "pipe chain" 'echo test | cat | cat' |
| 830 | +compare_posix_output "mixed or and" 'false || true && echo yes' |
| 831 | + |
| 832 | +section "198. PARENTHESES AND BRACES" |
| 833 | + |
| 834 | +compare_posix_output "paren group" '(echo a; echo b)' |
| 835 | +compare_posix_output "brace group" '{ echo a; echo b; }' |
| 836 | +# Note: (( )) is arithmetic syntax. Compare exit codes. |
| 837 | +compare_posix_exit_code "double paren arithmetic" '((echo deep))' |
| 838 | +compare_posix_output "brace in brace" '{ { echo deep; }; }' |
| 839 | +compare_posix_output "paren in brace" '{ (echo sub); }' |
| 840 | +compare_posix_output "brace in paren" '({ echo brace; })' |
| 841 | + |
| 842 | +section "199. RESERVED WORD POSITIONS" |
| 843 | + |
| 844 | +compare_posix_output "if as arg" 'echo if' |
| 845 | +compare_posix_output "then as arg" 'echo then' |
| 846 | +compare_posix_output "else as arg" 'echo else' |
| 847 | +compare_posix_output "fi as arg" 'echo fi' |
| 848 | +compare_posix_output "for as arg" 'echo for' |
| 849 | +compare_posix_output "while as arg" 'echo while' |
| 850 | +compare_posix_output "case as arg" 'echo case' |
| 851 | +compare_posix_output "do as arg" 'echo do' |
| 852 | +compare_posix_output "done as arg" 'echo done' |
| 853 | + |
| 854 | +section "200. ASSIGNMENT VARIATIONS" |
| 855 | + |
| 856 | +compare_posix_output "simple assign" 'X=val; echo $X' |
| 857 | +compare_posix_output "empty assign" 'X=; echo "[$X]"' |
| 858 | +compare_posix_output "quoted assign" 'X="val"; echo $X' |
| 859 | +compare_posix_output "single quote assign" "X='val'; echo \$X" |
| 860 | +compare_posix_output "cmd sub assign" 'X=$(echo val); echo $X' |
| 861 | +compare_posix_output "arith assign" 'X=$((1+1)); echo $X' |
| 862 | +compare_posix_output "multi assign" 'X=1 Y=2; echo $X $Y' |
| 863 | +compare_posix_output "prefix assign" 'X=val sh -c "echo \$X"' |
| 864 | +compare_posix_output "no space assign" 'X=nospace; echo $X' |
| 865 | + |
| 866 | +section "201. PARAMETER EXPANSION EDGE CASES" |
| 867 | + |
| 868 | +compare_posix_output "unset default" 'echo ${UNSET_VAR_XYZ:-default}' |
| 869 | +compare_posix_output "set default" 'X=val; echo ${X:-default}' |
| 870 | +compare_posix_output "empty default" 'X=; echo ${X:-default}' |
| 871 | +compare_posix_output "unset alt" 'echo ${UNSET_VAR_XYZ:+alt}' |
| 872 | +compare_posix_output "set alt" 'X=val; echo ${X:+alt}' |
| 873 | +compare_posix_output "empty alt" 'X=; echo ${X:+alt}' |
| 874 | +compare_posix_output "unset assign" 'echo ${UNSET_VAR_ABC:=assigned}; echo $UNSET_VAR_ABC' |
| 875 | +compare_posix_output "set no assign" 'X=val; echo ${X:=new}; echo $X' |
| 876 | +compare_posix_output "length zero" 'X=; echo ${#X}' |
| 877 | +compare_posix_output "length five" 'X=hello; echo ${#X}' |
| 878 | + |
| 879 | +section "202. PATTERN MATCHING IN EXPANSION" |
| 880 | + |
| 881 | +compare_posix_output "suffix percent" 'X=file.txt; echo ${X%.txt}' |
| 882 | +compare_posix_output "suffix double" 'X=a.b.c; echo ${X%%.*}' |
| 883 | +compare_posix_output "prefix hash" 'X=prefix_name; echo ${X#prefix_}' |
| 884 | +compare_posix_output "prefix double" 'X=a.b.c; echo ${X##*.}' |
| 885 | +compare_posix_output "no match suffix" 'X=file.txt; echo ${X%.jpg}' |
| 886 | +compare_posix_output "no match prefix" 'X=file.txt; echo ${X#img_}' |
| 887 | +compare_posix_output "star suffix" 'X=hello; echo ${X%l*}' |
| 888 | +compare_posix_output "star prefix" 'X=hello; echo ${X#*l}' |
| 889 | +compare_posix_output "question suffix" 'X=hello; echo ${X%?}' |
| 890 | +compare_posix_output "question prefix" 'X=hello; echo ${X#?}' |
| 891 | + |
| 892 | +section "203. ARITHMETIC OPERATORS" |
| 893 | + |
| 894 | +compare_posix_output "arith add" 'echo $((5 + 3))' |
| 895 | +compare_posix_output "arith sub" 'echo $((5 - 3))' |
| 896 | +compare_posix_output "arith mul" 'echo $((5 * 3))' |
| 897 | +compare_posix_output "arith div" 'echo $((15 / 3))' |
| 898 | +compare_posix_output "arith mod" 'echo $((17 % 5))' |
| 899 | +compare_posix_output "arith neg" 'echo $((-5))' |
| 900 | +compare_posix_output "arith paren" 'echo $(((2 + 3) * 4))' |
| 901 | +compare_posix_output "arith var" 'X=5; echo $((X + 1))' |
| 902 | +compare_posix_output "arith nested" 'echo $(($((1+1)) + $((2+2))))' |
| 903 | +compare_posix_output "arith zero div" 'echo $((0 / 1))' |
| 904 | + |
| 905 | +section "204. ARITHMETIC COMPARISONS" |
| 906 | + |
| 907 | +compare_posix_output "arith lt true" 'echo $((1 < 2))' |
| 908 | +compare_posix_output "arith lt false" 'echo $((2 < 1))' |
| 909 | +compare_posix_output "arith gt true" 'echo $((2 > 1))' |
| 910 | +compare_posix_output "arith gt false" 'echo $((1 > 2))' |
| 911 | +compare_posix_output "arith le true" 'echo $((1 <= 1))' |
| 912 | +compare_posix_output "arith ge true" 'echo $((1 >= 1))' |
| 913 | +compare_posix_output "arith eq true" 'echo $((5 == 5))' |
| 914 | +compare_posix_output "arith eq false" 'echo $((5 == 6))' |
| 915 | +compare_posix_output "arith ne true" 'echo $((5 != 6))' |
| 916 | +compare_posix_output "arith ne false" 'echo $((5 != 5))' |
| 917 | + |
| 918 | +section "205. ARITHMETIC LOGICAL" |
| 919 | + |
| 920 | +compare_posix_output "arith and true" 'echo $((1 && 1))' |
| 921 | +compare_posix_output "arith and false" 'echo $((1 && 0))' |
| 922 | +compare_posix_output "arith or true" 'echo $((0 || 1))' |
| 923 | +compare_posix_output "arith or false" 'echo $((0 || 0))' |
| 924 | +compare_posix_output "arith not true" 'echo $((!0))' |
| 925 | +compare_posix_output "arith not false" 'echo $((!1))' |
| 926 | +compare_posix_output "arith ternary t" 'echo $((1 ? 10 : 20))' |
| 927 | +compare_posix_output "arith ternary f" 'echo $((0 ? 10 : 20))' |
| 928 | +compare_posix_output "arith complex" 'echo $(((1 && 1) || 0))' |
| 929 | +compare_posix_output "arith nested log" 'echo $((!(0 || 0)))' |
| 930 | + |
| 931 | +section "206. TEST NUMERIC OPERATORS" |
| 932 | + |
| 933 | +compare_posix_output "test eq true" '[ 5 -eq 5 ]; echo $?' |
| 934 | +compare_posix_output "test eq false" '[ 5 -eq 6 ]; echo $?' |
| 935 | +compare_posix_output "test ne true" '[ 5 -ne 6 ]; echo $?' |
| 936 | +compare_posix_output "test ne false" '[ 5 -ne 5 ]; echo $?' |
| 937 | +compare_posix_output "test lt true" '[ 3 -lt 5 ]; echo $?' |
| 938 | +compare_posix_output "test lt false" '[ 5 -lt 3 ]; echo $?' |
| 939 | +compare_posix_output "test le true" '[ 5 -le 5 ]; echo $?' |
| 940 | +compare_posix_output "test gt true" '[ 5 -gt 3 ]; echo $?' |
| 941 | +compare_posix_output "test ge true" '[ 5 -ge 5 ]; echo $?' |
| 942 | +compare_posix_output "test zero" '[ 0 -eq 0 ]; echo $?' |
| 943 | + |
| 944 | +section "207. TEST STRING OPERATORS" |
| 945 | + |
| 946 | +compare_posix_output "test str eq true" '[ "abc" = "abc" ]; echo $?' |
| 947 | +compare_posix_output "test str eq false" '[ "abc" = "def" ]; echo $?' |
| 948 | +compare_posix_output "test str ne true" '[ "abc" != "def" ]; echo $?' |
| 949 | +compare_posix_output "test str ne false" '[ "abc" != "abc" ]; echo $?' |
| 950 | +compare_posix_output "test z true" '[ -z "" ]; echo $?' |
| 951 | +compare_posix_output "test z false" '[ -z "x" ]; echo $?' |
| 952 | +compare_posix_output "test n true" '[ -n "x" ]; echo $?' |
| 953 | +compare_posix_output "test n false" '[ -n "" ]; echo $?' |
| 954 | +compare_posix_output "test str space" '[ "a b" = "a b" ]; echo $?' |
| 955 | +compare_posix_output "test str empty" '[ "" = "" ]; echo $?' |
| 956 | + |
| 957 | +section "208. TEST FILE OPERATORS" |
| 958 | + |
| 959 | +compare_posix_output "test e exist" '[ -e /tmp ]; echo $?' |
| 960 | +compare_posix_output "test e noexist" '[ -e /nonexistent_xyz ]; echo $?' |
| 961 | +compare_posix_output "test f file" '[ -f /etc/passwd ]; echo $?' |
| 962 | +compare_posix_output "test f dir" '[ -f /tmp ]; echo $?' |
| 963 | +compare_posix_output "test d dir" '[ -d /tmp ]; echo $?' |
| 964 | +compare_posix_output "test d file" '[ -d /etc/passwd ]; echo $?' |
| 965 | +compare_posix_output "test r read" '[ -r /etc/passwd ]; echo $?' |
| 966 | +compare_posix_output "test w write" '[ -w /tmp ]; echo $?' |
| 967 | +compare_posix_output "test x exec" '[ -x /bin/sh ]; echo $?' |
| 968 | +compare_posix_output "test s size" '[ -s /etc/passwd ]; echo $?' |
| 969 | + |
| 970 | +section "209. TEST LOGICAL OPERATORS" |
| 971 | + |
| 972 | +compare_posix_output "test not true" '[ ! -f /nonexistent ]; echo $?' |
| 973 | +compare_posix_output "test not false" '[ ! -d /tmp ]; echo $?' |
| 974 | +compare_posix_output "test and true" '[ -d /tmp ] && [ -f /etc/passwd ]; echo $?' |
| 975 | +compare_posix_output "test and false" '[ -d /tmp ] && [ -f /nonexistent ]; echo $?' |
| 976 | +compare_posix_output "test or true" '[ -f /nonexistent ] || [ -d /tmp ]; echo $?' |
| 977 | +compare_posix_output "test or false" '[ -f /nonexistent ] || [ -d /nonexistent ]; echo $?' |
| 978 | +compare_posix_output "test complex" '[ -d /tmp ] && [ ! -f /nonexistent ]; echo $?' |
| 979 | +compare_posix_output "test chain" '[ 1 -eq 1 ] && [ 2 -eq 2 ] && [ 3 -eq 3 ]; echo $?' |
| 980 | + |
| 981 | +section "210. HEREDOC VARIATIONS" |
| 982 | + |
| 983 | +compare_posix_output "heredoc simple" 'cat <<EOF |
| 984 | +line |
| 985 | +EOF' |
| 986 | +compare_posix_output "heredoc multi" 'cat <<EOF |
| 987 | +one |
| 988 | +two |
| 989 | +three |
| 990 | +EOF' |
| 991 | +compare_posix_output "heredoc empty" 'cat <<EOF |
| 992 | +EOF |
| 993 | +echo done' |
| 994 | +compare_posix_output "heredoc expand" 'X=val; cat <<EOF |
| 995 | +$X |
| 996 | +EOF' |
| 997 | +compare_posix_output "heredoc no expand" "cat <<'EOF' |
| 998 | +\$X |
| 999 | +EOF" |
| 1000 | +compare_posix_output "heredoc indent" 'cat <<-EOF |
| 1001 | + tab |
| 1002 | +EOF' |
| 1003 | +compare_posix_output "heredoc special" 'cat <<EOF |
| 1004 | +* $ @ ! |
| 1005 | +EOF' |
| 1006 | + |
| 1007 | +section "211. IFS WORD SPLITTING" |
| 1008 | + |
| 1009 | +compare_posix_output "ifs split colon" 'IFS=:; x="a:b:c"; set -- $x; echo $#' |
| 1010 | +compare_posix_output "ifs split space" 'IFS=" "; x="a b c"; set -- $x; echo $#' |
| 1011 | +compare_posix_output "ifs split multi" 'IFS=":;"; x="a:b;c"; set -- $x; echo $#' |
| 1012 | +compare_posix_output "ifs default split" 'x="a b c"; set -- $x; echo $#' |
| 1013 | +compare_posix_output "ifs empty no split" 'IFS=""; x="a b"; set -- $x; echo $#' |
| 1014 | +compare_posix_output "ifs unset default" 'unset IFS; x="a b"; set -- $x; echo $#' |
| 1015 | +compare_posix_output "ifs in for" 'IFS=:; for i in a:b:c; do echo $i; done' |
| 1016 | +compare_posix_output "ifs restore" 'OLD="$IFS"; IFS=:; IFS="$OLD"; echo ok' |
| 1017 | + |
| 1018 | +section "212. WORD SPLITTING" |
| 1019 | + |
| 1020 | +compare_posix_output "split unquoted" 'x="a b c"; set -- $x; echo $#' |
| 1021 | +compare_posix_output "split quoted" 'x="a b c"; set -- "$x"; echo $#' |
| 1022 | +compare_posix_output "split ifs" 'IFS=:; x="a:b:c"; set -- $x; echo $#' |
| 1023 | +compare_posix_output "split empty" 'x=""; set -- $x; echo $#' |
| 1024 | +compare_posix_output "split whitespace" 'x=" a b "; set -- $x; echo $#' |
| 1025 | +compare_posix_output "split preserve" 'x="a b"; echo "$x"' |
| 1026 | +compare_posix_output "split multiple" 'x="a b" y="c d"; set -- $x $y; echo $#' |
| 1027 | + |
| 1028 | +section "213. PATHNAME EXPANSION" |
| 1029 | + |
| 1030 | +compare_posix_output "glob star" 'set -f; echo *; set +f' |
| 1031 | +compare_posix_output "glob question" 'set -f; echo ?; set +f' |
| 1032 | +compare_posix_output "glob bracket" 'set -f; echo [abc]; set +f' |
| 1033 | +compare_posix_output "glob quoted" 'echo "*"' |
| 1034 | +compare_posix_output "glob escaped" 'echo \*' |
| 1035 | +compare_posix_output "glob noglob" 'set -f; echo *.txt; set +f' |
| 1036 | +compare_posix_output "glob in var" 'x="*"; echo "$x"' |
| 1037 | + |
| 1038 | +section "214. TILDE EXPANSION" |
| 1039 | + |
| 1040 | +compare_posix_output "tilde alone" 'echo ~ | grep -c "/"' |
| 1041 | +compare_posix_output "tilde slash" 'echo ~/ | grep -c "/"' |
| 1042 | +compare_posix_output "tilde quoted" 'echo "~"' |
| 1043 | +compare_posix_output "tilde in var" 'x=~; echo $x | grep -c "/"' |
| 1044 | +compare_posix_output "tilde assign" 'HOME=/tmp; echo ~ | grep -c "/"' |
| 1045 | + |
| 1046 | +section "215. COMMAND SUBSTITUTION NESTING" |
| 1047 | + |
| 1048 | +compare_posix_output "cmd sub simple" 'echo $(echo hello)' |
| 1049 | +compare_posix_output "cmd sub nested" 'echo $(echo $(echo deep))' |
| 1050 | +compare_posix_output "cmd sub triple" 'echo $(echo $(echo $(echo triple)))' |
| 1051 | +compare_posix_output "cmd sub arith" 'echo $(echo $((1+1)))' |
| 1052 | +compare_posix_output "cmd sub var" 'X=val; echo $(echo $X)' |
| 1053 | +compare_posix_output "cmd sub quote" 'echo "$(echo "quoted")"' |
| 1054 | +compare_posix_output "backtick simple" 'echo `echo hello`' |
| 1055 | +compare_posix_output "backtick nested" 'echo `echo \`echo deep\``' |
| 1056 | + |
| 1057 | +section "216. PIPELINE ALTERNATIVES" |
| 1058 | + |
| 1059 | +compare_posix_output "pipe chain filter" 'echo test | grep test | cat' |
| 1060 | +compare_posix_output "subshell pipe" '(echo test) | cat' |
| 1061 | +compare_posix_output "brace pipe" '{ echo test; } | cat' |
| 1062 | +compare_posix_output "pipe to head" 'printf "a\nb\nc\n" | head -2 | wc -l' |
| 1063 | + |
| 1064 | +section "217. EXIT STATUS" |
| 1065 | + |
| 1066 | +compare_posix_output "exit 0" 'exit 0' |
| 1067 | +compare_posix_output "exit 1" '(exit 1); echo $?' |
| 1068 | +compare_posix_output "exit 255" '(exit 255); echo $?' |
| 1069 | +compare_posix_output "true status" 'true; echo $?' |
| 1070 | +compare_posix_output "false status" 'false; echo $?' |
| 1071 | +compare_posix_output "cmd not found" 'nonexistent_cmd_xyz 2>/dev/null; echo $?' |
| 1072 | +compare_posix_output "pipe status" 'false | true; echo $?' |
| 1073 | +compare_posix_output "and status" 'true && true; echo $?' |
| 1074 | +compare_posix_output "or status" 'false || true; echo $?' |
| 1075 | +compare_posix_output "not status" '! false; echo $?' |
| 1076 | + |
| 1077 | +section "218. SIGNAL HANDLING" |
| 1078 | + |
| 1079 | +# Note: trap output may vary by environment - test exit code |
| 1080 | +compare_posix_exit_code "trap list" 'trap >/dev/null 2>&1' |
| 1081 | +compare_posix_output "trap set" 'trap "echo caught" INT; trap | grep -c INT || echo 0' |
| 1082 | +compare_posix_output "trap reset" 'trap "echo x" INT; trap - INT; trap | grep -c INT || echo 0' |
| 1083 | +compare_posix_output "trap ignore" 'trap "" INT; trap | grep -c INT || echo 0' |
| 1084 | +compare_posix_output "kill signal" 'kill -l | head -1 | grep -c "[A-Z]" || echo 1' |
| 1085 | + |
| 1086 | +section "219. ADDITIONAL TESTS" |
| 1087 | + |
| 1088 | +compare_posix_output "pipeline exit" 'true | false; echo $?' |
| 1089 | +compare_posix_output "pipeline true" 'true | true; echo $?' |
| 1090 | +compare_posix_output "negation pipe" '! false | true; echo $?' |
| 1091 | +compare_posix_output "subshell exit" '(exit 42); echo $?' |
| 1092 | +compare_posix_output "brace exit" '{ true; }; echo $?' |
| 1093 | + |
| 1094 | +section "220. ENVIRONMENT VARIABLES" |
| 1095 | + |
| 1096 | +compare_posix_output "env home" 'echo ${HOME:-unset} | grep -c "/"' |
| 1097 | +compare_posix_output "env path" 'echo ${PATH:-unset} | grep -c ":"' |
| 1098 | +compare_posix_output "env pwd" 'echo ${PWD:-unset} | grep -c "/"' |
| 1099 | +compare_posix_output "env shell" 'echo ${SHELL:-unset} | grep -c "/" || echo 1' |
| 1100 | +compare_posix_output "export var" 'export X=val; sh -c "echo \$X"' |
| 1101 | +compare_posix_output "prefix assign" 'X=val sh -c "echo \$X"' |
| 1102 | +compare_posix_output "unset env" 'export X=val; unset X; echo ${X:-unset}' |
| 1103 | + |
| 1104 | +section "221. SPECIAL VARIABLES" |
| 1105 | + |
| 1106 | +compare_posix_output "dollar zero" 'echo ${0:-shell} | grep -c "."' |
| 1107 | +compare_posix_output "dollar hash" 'set -- a b c; echo $#' |
| 1108 | +compare_posix_output "dollar question" 'true; echo $?' |
| 1109 | +compare_posix_output "dollar dollar" 'echo $$ | grep -c "[0-9]"' |
| 1110 | +compare_posix_output "dollar underscore" 'echo test; echo ${_:-none}' |
| 1111 | +compare_posix_output "dollar at" 'set -- a b c; echo "$@"' |
| 1112 | +compare_posix_output "dollar star" 'set -- a b c; echo "$*"' |
| 1113 | +compare_posix_output "dollar minus" 'echo $- | grep -c "."' |
| 1114 | + |
| 1115 | +section "222. POSITIONAL PARAMETERS" |
| 1116 | + |
| 1117 | +compare_posix_output "pos one" 'set -- a b c; echo $1' |
| 1118 | +compare_posix_output "pos two" 'set -- a b c; echo $2' |
| 1119 | +compare_posix_output "pos three" 'set -- a b c; echo $3' |
| 1120 | +compare_posix_output "pos shift" 'set -- a b c; shift; echo $1' |
| 1121 | +compare_posix_output "pos shift n" 'set -- a b c d e; shift 3; echo $1' |
| 1122 | +compare_posix_output "pos set" 'set -- x y z; echo $1 $2 $3' |
| 1123 | +compare_posix_output "pos clear" 'set -- a b c; set --; echo ${1:-empty}' |
| 1124 | +compare_posix_output "pos count" 'set -- a b c d e; echo $#' |
| 1125 | +compare_posix_output "pos all at" 'set -- a b c; for x in "$@"; do echo $x; done | wc -l' |
| 1126 | +compare_posix_output "pos all star" 'set -- a b c; echo "$*" | wc -w' |
| 1127 | + |
| 1128 | +section "223. FUNCTION DEFINITION STYLES" |
| 1129 | + |
| 1130 | +compare_posix_output "func keyword" 'f() { echo hello; }; f' |
| 1131 | +compare_posix_output "func oneline" 'f() { echo one; }; f' |
| 1132 | +compare_posix_output "func multiline" 'f() { |
| 1133 | +echo multi |
| 1134 | +}; f' |
| 1135 | +compare_posix_output "func with args" 'f() { echo $1 $2; }; f a b' |
| 1136 | +compare_posix_output "func return" 'f() { return 5; }; f; echo $?' |
| 1137 | +compare_posix_output "func local var" 'X=outer; f() { X=inner; }; f; echo $X' |
| 1138 | +compare_posix_output "func recursive" 'f() { [ $1 -eq 0 ] && echo done || f $(($1-1)); }; f 3' |
| 1139 | + |
| 1140 | +section "224. FUNCTION SCOPE" |
| 1141 | + |
| 1142 | +compare_posix_output "func global var" 'X=global; f() { echo $X; }; f' |
| 1143 | +compare_posix_output "func modify var" 'X=old; f() { X=new; }; f; echo $X' |
| 1144 | +compare_posix_output "func args" 'f() { echo $#; }; f a b c' |
| 1145 | +compare_posix_output "func positional" 'f() { echo $1; }; set -- x; f a; echo $1' |
| 1146 | +compare_posix_output "func in subshell" 'f() { echo func; }; (f)' |
| 1147 | +compare_posix_output "func in pipeline" 'f() { echo test; }; f | cat' |
| 1148 | +compare_posix_output "func redefine" 'f() { echo one; }; f; f() { echo two; }; f' |
| 1149 | + |
| 1150 | +section "225. ALIAS BASICS" |
| 1151 | + |
| 1152 | +compare_posix_output "alias define" 'alias x=echo 2>/dev/null; echo ok' |
| 1153 | +compare_posix_output "alias list" 'alias 2>/dev/null; echo $?' |
| 1154 | +compare_posix_output "unalias" 'alias x=echo 2>/dev/null; unalias x 2>/dev/null; echo $?' |
| 1155 | +compare_posix_output "unalias all" 'unalias -a 2>/dev/null; echo $?' |
| 1156 | + |
| 1157 | +section "226. CONTROL STRUCTURES COMPREHENSIVE" |
| 1158 | + |
| 1159 | +compare_posix_output "if basic" 'if true; then echo yes; fi' |
| 1160 | +compare_posix_output "if else" 'if false; then echo no; else echo yes; fi' |
| 1161 | +compare_posix_output "if elif" 'if false; then echo 1; elif true; then echo 2; fi' |
| 1162 | +compare_posix_output "if nested" 'if true; then if true; then echo deep; fi; fi' |
| 1163 | +compare_posix_output "for basic" 'for i in 1 2 3; do echo $i; done | wc -l' |
| 1164 | +compare_posix_output "for empty" 'for i in; do echo $i; done; echo done' |
| 1165 | +compare_posix_output "while basic" 'n=3; while [ $n -gt 0 ]; do echo $n; n=$((n-1)); done | wc -l' |
| 1166 | +compare_posix_output "until basic" 'n=0; until [ $n -eq 3 ]; do n=$((n+1)); done; echo $n' |
| 1167 | +compare_posix_output "case basic" 'case x in x) echo yes;; esac' |
| 1168 | +compare_posix_output "case default" 'case x in y) echo no;; *) echo default;; esac' |
| 1169 | + |
| 1170 | +section "227. BREAK AND CONTINUE" |
| 1171 | + |
| 1172 | +compare_posix_output "break simple" 'for i in 1 2 3; do [ $i -eq 2 ] && break; echo $i; done' |
| 1173 | +compare_posix_output "continue simple" 'for i in 1 2 3; do [ $i -eq 2 ] && continue; echo $i; done' |
| 1174 | +compare_posix_output "break nested" 'for i in 1 2; do for j in a b; do break; done; echo $i; done' |
| 1175 | +compare_posix_output "break 2" 'for i in 1 2; do for j in a b; do break 2; done; done; echo done' |
| 1176 | +compare_posix_output "continue nested" 'for i in 1 2; do for j in a b; do continue 2; done; echo no; done; echo done' |
| 1177 | + |
| 1178 | +section "228. RETURN AND EXIT" |
| 1179 | + |
| 1180 | +compare_posix_output "return 0" 'f() { return 0; }; f; echo $?' |
| 1181 | +compare_posix_output "return 1" 'f() { return 1; }; f; echo $?' |
| 1182 | +compare_posix_output "return 255" 'f() { return 255; }; f; echo $?' |
| 1183 | +compare_posix_output "exit subshell" '(exit 5); echo $?' |
| 1184 | +compare_posix_output "exit brace" '{ exit 0; }; echo unreached' |
| 1185 | +compare_posix_output "return implicit" 'f() { true; }; f; echo $?' |
| 1186 | + |
| 1187 | +section "229. COMPOUND COMMANDS" |
| 1188 | + |
| 1189 | +compare_posix_output "paren list" '(echo a; echo b) | wc -l' |
| 1190 | +compare_posix_output "brace list" '{ echo a; echo b; } | wc -l' |
| 1191 | +compare_posix_output "if in paren" '(if true; then echo yes; fi)' |
| 1192 | +compare_posix_output "for in brace" '{ for i in 1 2; do echo $i; done; } | wc -l' |
| 1193 | +compare_posix_output "while in paren" '(n=2; while [ $n -gt 0 ]; do echo $n; n=$((n-1)); done) | wc -l' |
| 1194 | +compare_posix_output "case in brace" '{ case x in x) echo yes;; esac; }' |
| 1195 | +compare_posix_output "func in paren" '(f() { echo func; }; f)' |
| 1196 | + |
| 1197 | +section "230. COMPLEX COMMAND COMBINATIONS" |
| 1198 | + |
| 1199 | +compare_posix_output "for pipe filter" 'for i in 1 2 3 4 5; do echo $i; done | head -3 | wc -l' |
| 1200 | +compare_posix_output "case in for" 'for x in a b c; do case $x in a) echo first;; esac; done' |
| 1201 | +compare_posix_output "if in while" 'n=2; while [ $n -gt 0 ]; do if [ $n -eq 1 ]; then echo one; fi; n=$((n-1)); done' |
| 1202 | +compare_posix_output "nested for" 'for i in 1 2; do for j in a b; do echo $i$j; done; done | wc -l' |
| 1203 | +compare_posix_output "subshell in for" 'for i in 1 2; do (echo $i); done | wc -l' |
| 1204 | +compare_posix_output "func in for" 'f() { echo $1; }; for i in a b c; do f $i; done | wc -l' |
| 1205 | +compare_posix_output "pipe to sort" 'printf "c\na\nb\n" | sort | head -1' |
| 1206 | +# Note: (( )) is arithmetic syntax. Compare exit codes. |
| 1207 | +compare_posix_exit_code "nested subshell arithmetic" '((echo deep))' |
| 1208 | + |
| 1209 | +section "231. EXPR COMMAND" |
| 1210 | + |
| 1211 | +compare_posix_output "expr add" 'expr 5 + 3' |
| 1212 | +compare_posix_output "expr sub" 'expr 10 - 3' |
| 1213 | +compare_posix_output "expr mul" 'expr 4 \* 5' |
| 1214 | +compare_posix_output "expr div" 'expr 20 / 4' |
| 1215 | +compare_posix_output "expr mod" 'expr 17 % 5' |
| 1216 | +compare_posix_output "expr compare lt" 'expr 3 \< 5' |
| 1217 | +compare_posix_output "expr compare gt" 'expr 5 \> 3' |
| 1218 | +compare_posix_output "expr compare eq" 'expr 5 = 5' |
| 1219 | +compare_posix_output "expr string" 'expr length hello' |
| 1220 | +compare_posix_output "expr substr" 'expr substr hello 2 3' |
| 1221 | + |
| 1222 | +section "232. BASENAME AND DIRNAME" |
| 1223 | + |
| 1224 | +compare_posix_output "basename simple" 'basename /path/to/file' |
| 1225 | +compare_posix_output "basename suffix" 'basename /path/to/file.txt .txt' |
| 1226 | +compare_posix_output "basename no path" 'basename file.txt' |
| 1227 | +compare_posix_output "dirname simple" 'dirname /path/to/file' |
| 1228 | +compare_posix_output "dirname root" 'dirname /file' |
| 1229 | +compare_posix_output "dirname relative" 'dirname file' |
| 1230 | +compare_posix_output "dirname dot" 'dirname ./file' |
| 1231 | +compare_posix_output "dirname trailing" 'dirname /path/to/' |
| 1232 | + |
| 1233 | +section "233. CUT COMMAND" |
| 1234 | + |
| 1235 | +compare_posix_output "cut field" 'echo "a:b:c" | cut -d: -f2' |
| 1236 | +compare_posix_output "cut fields" 'echo "a:b:c" | cut -d: -f1,3' |
| 1237 | +compare_posix_output "cut chars" 'echo "hello" | cut -c1-3' |
| 1238 | +compare_posix_output "cut char single" 'echo "hello" | cut -c2' |
| 1239 | +compare_posix_output "cut range" 'echo "a:b:c:d" | cut -d: -f2-3' |
| 1240 | + |
| 1241 | +section "234. TR COMMAND" |
| 1242 | + |
| 1243 | +compare_posix_output "tr simple" 'echo abc | tr a-z A-Z' |
| 1244 | +compare_posix_output "tr delete" 'echo "a1b2c3" | tr -d 0-9' |
| 1245 | +compare_posix_output "tr squeeze" 'echo "aabbcc" | tr -s a-z' |
| 1246 | +compare_posix_output "tr single" 'echo abc | tr a x' |
| 1247 | +compare_posix_output "tr complement" 'echo "a1b2" | tr -cd a-z' |
| 1248 | + |
| 1249 | +section "235. SORT COMMAND" |
| 1250 | + |
| 1251 | +compare_posix_output "sort lines" 'printf "c\na\nb\n" | sort' |
| 1252 | +compare_posix_output "sort reverse" 'printf "a\nb\nc\n" | sort -r' |
| 1253 | +compare_posix_output "sort numeric" 'printf "10\n2\n1\n" | sort -n' |
| 1254 | +compare_posix_output "sort unique" 'printf "a\na\nb\n" | sort -u' |
| 1255 | +compare_posix_output "sort first" 'printf "c\na\nb\n" | sort | head -1' |
| 1256 | + |
| 1257 | +section "236. UNIQ COMMAND" |
| 1258 | + |
| 1259 | +compare_posix_output "uniq simple" 'printf "a\na\nb\n" | uniq' |
| 1260 | +compare_posix_output "uniq count" 'printf "a\na\nb\n" | uniq -c | wc -l' |
| 1261 | +compare_posix_output "uniq duplicate" 'printf "a\na\nb\n" | uniq -d' |
| 1262 | +compare_posix_output "uniq unique" 'printf "a\na\nb\n" | uniq -u' |
| 1263 | + |
| 1264 | +section "237. WC COMMAND" |
| 1265 | + |
| 1266 | +compare_posix_output "wc lines" 'printf "a\nb\nc\n" | wc -l' |
| 1267 | +compare_posix_output "wc words" 'echo "one two three" | wc -w' |
| 1268 | +compare_posix_output "wc chars" 'echo "hello" | wc -c' |
| 1269 | +compare_posix_output "wc empty" 'echo "" | wc -l' |
| 1270 | + |
| 1271 | +section "238. HEAD AND TAIL" |
| 1272 | + |
| 1273 | +compare_posix_output "head default" 'seq 1 20 | head | wc -l' |
| 1274 | +compare_posix_output "head n" 'seq 1 10 | head -3' |
| 1275 | +compare_posix_output "tail default" 'seq 1 20 | tail | wc -l' |
| 1276 | +compare_posix_output "tail n" 'seq 1 10 | tail -3' |
| 1277 | +compare_posix_output "head tail combo" 'seq 1 10 | head -5 | tail -1' |
| 1278 | + |
| 1279 | +section "239. GREP PATTERNS" |
| 1280 | + |
| 1281 | +compare_posix_output "grep simple" 'echo hello | grep hello' |
| 1282 | +compare_posix_output "grep no match" 'echo hello | grep xyz; echo $?' |
| 1283 | +compare_posix_output "grep count" 'printf "a\nb\na\n" | grep -c a' |
| 1284 | +compare_posix_output "grep ignore case" 'echo HELLO | grep -i hello' |
| 1285 | +compare_posix_output "grep invert" 'printf "a\nb\n" | grep -v a' |
| 1286 | +compare_posix_output "grep line" 'printf "abc\ndef\n" | grep -n abc' |
| 1287 | + |
| 1288 | +section "240. SED BASICS" |
| 1289 | + |
| 1290 | +compare_posix_output "sed substitute" 'echo hello | sed "s/hello/world/"' |
| 1291 | +compare_posix_output "sed global" 'echo "aaa" | sed "s/a/b/g"' |
| 1292 | +compare_posix_output "sed delete" 'printf "a\nb\nc\n" | sed "1d" | wc -l' |
| 1293 | +compare_posix_output "sed print" 'printf "a\nb\n" | sed -n "1p"' |
| 1294 | +compare_posix_output "sed range" 'printf "a\nb\nc\n" | sed "1,2d"' |
| 1295 | + |
| 1296 | +section "241. AWK BASICS" |
| 1297 | + |
| 1298 | +compare_posix_output "awk print" 'echo "a b c" | awk "{print \$2}"' |
| 1299 | +compare_posix_output "awk field" 'echo "a:b:c" | awk -F: "{print \$2}"' |
| 1300 | +compare_posix_output "awk NF" 'echo "a b c" | awk "{print NF}"' |
| 1301 | +compare_posix_output "awk NR" 'printf "a\nb\n" | awk "{print NR}"' |
| 1302 | +compare_posix_output "awk math" 'echo "5 3" | awk "{print \$1 + \$2}"' |
| 1303 | + |
| 1304 | +section "242. TEE COMMAND" |
| 1305 | + |
| 1306 | +compare_posix_output "tee output" 'echo test | tee /dev/null' |
| 1307 | +compare_posix_output "tee passthrough" 'echo hello | tee /dev/null | cat' |
| 1308 | + |
| 1309 | +section "243. XARGS BASICS" |
| 1310 | + |
| 1311 | +compare_posix_output "xargs echo" 'echo "a b c" | xargs echo' |
| 1312 | +compare_posix_output "xargs n1" 'printf "a\nb\nc\n" | xargs -n1 echo | wc -l' |
| 1313 | + |
| 1314 | +section "244. FIND BASICS" |
| 1315 | + |
| 1316 | +compare_posix_output "find type d" 'find /tmp -maxdepth 1 -type d 2>/dev/null | head -1 | grep -c "/"' |
| 1317 | +compare_posix_output "find name" 'find /etc -maxdepth 1 -name "passwd" 2>/dev/null | grep -c passwd || echo 0' |
| 1318 | + |
| 1319 | +section "245. TEST COMMAND FORMS" |
| 1320 | + |
| 1321 | +compare_posix_output "test bracket" '[ 1 -eq 1 ]; echo $?' |
| 1322 | +compare_posix_output "test keyword" 'test 1 -eq 1; echo $?' |
| 1323 | +compare_posix_output "test string" '[ "a" = "a" ]; echo $?' |
| 1324 | +compare_posix_output "test empty" '[ -z "" ]; echo $?' |
| 1325 | +compare_posix_output "test nonempty" '[ -n "x" ]; echo $?' |
| 1326 | +compare_posix_output "test not" '[ ! 1 -eq 2 ]; echo $?' |
| 1327 | +compare_posix_output "test and ext" '[ 1 -eq 1 -a 2 -eq 2 ]; echo $?' |
| 1328 | +compare_posix_output "test or ext" '[ 1 -eq 2 -o 2 -eq 2 ]; echo $?' |
| 1329 | + |
| 1330 | +section "246. ARITHMETIC BITWISE" |
| 1331 | + |
| 1332 | +compare_posix_output "arith and" 'echo $((5 & 3))' |
| 1333 | +compare_posix_output "arith or" 'echo $((5 | 3))' |
| 1334 | +compare_posix_output "arith xor" 'echo $((5 ^ 3))' |
| 1335 | +compare_posix_output "arith not" 'echo $((~0))' |
| 1336 | +compare_posix_output "arith lshift" 'echo $((1 << 4))' |
| 1337 | +compare_posix_output "arith rshift" 'echo $((16 >> 2))' |
| 1338 | + |
| 1339 | +section "247. COMPLEX CASE PATTERNS" |
| 1340 | + |
| 1341 | +compare_posix_output "case or pattern" 'case abc in a*|b*) echo match;; esac' |
| 1342 | +compare_posix_output "case bracket" 'case a in [abc]) echo match;; esac' |
| 1343 | +compare_posix_output "case negbracket" 'case d in [!abc]) echo match;; esac' |
| 1344 | +compare_posix_output "case question" 'case ab in ??) echo two;; esac' |
| 1345 | +compare_posix_output "case star" 'case anything in *) echo match;; esac' |
| 1346 | +compare_posix_output "case empty" 'case "" in "") echo empty;; esac' |
| 1347 | +compare_posix_output "case number" 'case 42 in [0-9]*) echo num;; esac' |
| 1348 | + |
| 1349 | +section "248. COMPLEX FOR LOOPS" |
| 1350 | + |
| 1351 | +compare_posix_output "for glob safe" 'set -f; for i in *; do echo "$i"; done; set +f' |
| 1352 | +compare_posix_output "for break early" 'for i in 1 2 3 4 5; do echo $i; [ $i -eq 3 ] && break; done | wc -l' |
| 1353 | +compare_posix_output "for continue skip" 'for i in 1 2 3; do [ $i -eq 2 ] && continue; echo $i; done | wc -l' |
| 1354 | +compare_posix_output "for nested count" 'for i in 1 2; do for j in a b c; do echo x; done; done | wc -l' |
| 1355 | +compare_posix_output "for empty list" 'for i in; do echo never; done; echo done' |
| 1356 | +compare_posix_output "for single" 'for i in only; do echo $i; done' |
| 1357 | + |
| 1358 | +section "249. COMPLEX WHILE LOOPS" |
| 1359 | + |
| 1360 | +compare_posix_output "while decrement" 'n=5; while [ $n -gt 0 ]; do n=$((n-1)); done; echo $n' |
| 1361 | +compare_posix_output "while false" 'while false; do echo never; done; echo done' |
| 1362 | +compare_posix_output "while break" 'n=0; while true; do n=$((n+1)); [ $n -ge 3 ] && break; done; echo $n' |
| 1363 | +compare_posix_output "while nested" 'i=2; while [ $i -gt 0 ]; do j=2; while [ $j -gt 0 ]; do echo x; j=$((j-1)); done; i=$((i-1)); done | wc -l' |
| 1364 | +compare_posix_output "until true" 'until true; do echo never; done; echo done' |
| 1365 | +compare_posix_output "until count" 'n=0; until [ $n -ge 3 ]; do n=$((n+1)); done; echo $n' |
| 1366 | + |
| 1367 | +section "250. COMPLEX FUNCTIONS" |
| 1368 | + |
| 1369 | +compare_posix_output "func args count" 'f() { echo $#; }; f a b c d e' |
| 1370 | +compare_posix_output "func all args" 'f() { echo "$@"; }; f a b c' |
| 1371 | +compare_posix_output "func star args" 'f() { echo "$*"; }; f a b c' |
| 1372 | +compare_posix_output "func return val" 'f() { return 42; }; f; echo $?' |
| 1373 | +compare_posix_output "func modify global" 'x=old; f() { x=new; }; f; echo $x' |
| 1374 | +compare_posix_output "func recursive" 'f() { [ $1 -le 1 ] && echo 1 || echo $(($(f $(($1-1))) + $(f $(($1-2))))); }; f 5' |
| 1375 | +compare_posix_output "func in subshell" 'f() { echo inner; }; (f)' |
| 1376 | +compare_posix_output "func pipe" 'f() { echo test; }; f | cat' |
| 1377 | +compare_posix_output "func empty" 'f() { :; }; f; echo $?' |
| 1378 | +compare_posix_output "func chain" 'f() { echo $1; }; g() { f hello; }; g' |
| 1379 | + |
| 1380 | +section "251. SHELL ARITHMETIC EDGE CASES" |
| 1381 | + |
| 1382 | +compare_posix_output "arith octal" 'echo $((010))' |
| 1383 | +compare_posix_output "arith hex" 'echo $((0x10))' |
| 1384 | +compare_posix_output "arith unary plus" 'echo $((+5))' |
| 1385 | +compare_posix_output "arith unary minus" 'echo $((-5))' |
| 1386 | +compare_posix_output "arith double neg" 'echo $((--5))' |
| 1387 | +compare_posix_output "arith complex" 'echo $(((1+2)*(3+4)))' |
| 1388 | +compare_posix_output "arith assign" 'x=5; echo $((x=x+1)); echo $x' |
| 1389 | +compare_posix_output "arith incr" 'x=5; echo $((x+=1))' |
| 1390 | +compare_posix_output "arith decr" 'x=5; echo $((x-=1))' |
| 1391 | + |
| 1392 | +section "252. VARIABLE EDGE CASES" |
| 1393 | + |
| 1394 | +compare_posix_output "var underscore" '_x=val; echo $_x' |
| 1395 | +compare_posix_output "var number suffix" 'x1=val; echo $x1' |
| 1396 | +compare_posix_output "var long name" 'very_long_variable_name_here=val; echo $very_long_variable_name_here' |
| 1397 | +compare_posix_output "var empty val" 'x=; echo "[$x]"' |
| 1398 | +compare_posix_output "var space val" 'x="a b"; echo "$x"' |
| 1399 | +compare_posix_output "var newline val" 'x="a |
| 1400 | +b"; echo "$x" | wc -l' |
| 1401 | +compare_posix_output "var special chars" 'x="!@#"; echo "$x"' |
| 1402 | +compare_posix_output "var equals in val" 'x="a=b"; echo "$x"' |
| 1403 | + |
| 1404 | +section "253. QUOTING EDGE CASES" |
| 1405 | + |
| 1406 | +compare_posix_output "quote empty" 'echo ""' |
| 1407 | +compare_posix_output "quote space" 'echo " "' |
| 1408 | +compare_posix_output "quote tab" 'echo " "' |
| 1409 | +compare_posix_output "quote newline" 'echo " |
| 1410 | +"' |
| 1411 | +compare_posix_output "quote dollar" 'echo "\$"' |
| 1412 | +compare_posix_output "quote backslash" 'echo "\\"' |
| 1413 | +compare_posix_output "quote backtick" 'echo "\`"' |
| 1414 | +compare_posix_output "quote double" 'echo "\""' |
| 1415 | +compare_posix_output "single in double" 'echo "'"'"'"' |
| 1416 | +compare_posix_output "double in single" "echo '\"'" |
| 1417 | + |
| 1418 | +section "254. PARAMETER EXPANSION COMPREHENSIVE" |
| 1419 | + |
| 1420 | +compare_posix_output "param default unset" 'echo ${undef:-default}' |
| 1421 | +compare_posix_output "param default empty" 'x=; echo ${x:-default}' |
| 1422 | +compare_posix_output "param default set" 'x=val; echo ${x:-default}' |
| 1423 | +compare_posix_output "param alt unset" 'echo ${undef:+alt}' |
| 1424 | +compare_posix_output "param alt empty" 'x=; echo ${x:+alt}' |
| 1425 | +compare_posix_output "param alt set" 'x=val; echo ${x:+alt}' |
| 1426 | +compare_posix_output "param assign unset" 'unset y; echo ${y:=assigned}; echo $y' |
| 1427 | +compare_posix_output "param length" 'x=hello; echo ${#x}' |
| 1428 | +compare_posix_output "param suffix" 'x=file.txt; echo ${x%.txt}' |
| 1429 | +compare_posix_output "param prefix" 'x=prefix_name; echo ${x#prefix_}' |
| 1430 | + |
| 1431 | +section "255. REDIRECTION COMPREHENSIVE" |
| 1432 | + |
| 1433 | +compare_posix_output "redir stdout" 'echo test > /tmp/r$$; cat /tmp/r$$; rm /tmp/r$$' |
| 1434 | +compare_posix_output "redir append" 'echo a > /tmp/r$$; echo b >> /tmp/r$$; cat /tmp/r$$; rm /tmp/r$$' |
| 1435 | +compare_posix_output "redir stdin" 'echo test > /tmp/r$$; cat < /tmp/r$$; rm /tmp/r$$' |
| 1436 | +compare_posix_output "redir stderr" 'echo err >&2 2>/dev/null; echo ok' |
| 1437 | +compare_posix_output "redir fd dup" 'echo test 2>&1 | cat' |
| 1438 | +compare_posix_output "redir devnull" 'echo test > /dev/null; echo $?' |
| 1439 | +compare_posix_output "redir clobber" 'echo a > /tmp/r$$; echo b > /tmp/r$$; cat /tmp/r$$; rm /tmp/r$$' |
| 1440 | + |
| 1441 | +section "256. HEREDOC COMPREHENSIVE" |
| 1442 | + |
| 1443 | +compare_posix_output "heredoc basic" 'cat <<EOF |
| 1444 | +line |
| 1445 | +EOF' |
| 1446 | +compare_posix_output "heredoc multi" 'cat <<EOF |
| 1447 | +one |
| 1448 | +two |
| 1449 | +EOF' |
| 1450 | +compare_posix_output "heredoc var" 'x=val; cat <<EOF |
| 1451 | +$x |
| 1452 | +EOF' |
| 1453 | +compare_posix_output "heredoc quoted" "cat <<'EOF' |
| 1454 | +\$x |
| 1455 | +EOF" |
| 1456 | +compare_posix_output "heredoc tab strip" 'cat <<-EOF |
| 1457 | + indented |
| 1458 | +EOF' |
| 1459 | + |
| 1460 | +section "257. PIPELINE COMPREHENSIVE" |
| 1461 | + |
| 1462 | +compare_posix_output "pipe two" 'echo test | cat' |
| 1463 | +compare_posix_output "pipe three" 'echo test | cat | cat' |
| 1464 | +compare_posix_output "pipe four" 'echo test | cat | cat | cat' |
| 1465 | +compare_posix_output "pipe filter" 'echo hello | grep h' |
| 1466 | +compare_posix_output "pipe transform" 'echo abc | tr a-z A-Z' |
| 1467 | +compare_posix_output "pipe count" 'printf "a\nb\nc\n" | wc -l' |
| 1468 | +compare_posix_output "pipe subshell" '(echo test) | cat' |
| 1469 | +compare_posix_output "pipe brace" '{ echo test; } | cat' |
| 1470 | + |
| 1471 | +section "258. LOGICAL OPERATORS COMPREHENSIVE" |
| 1472 | + |
| 1473 | +compare_posix_output "and tt" 'true && true; echo $?' |
| 1474 | +compare_posix_output "and tf" 'true && false; echo $?' |
| 1475 | +compare_posix_output "and ft" 'false && true; echo $?' |
| 1476 | +compare_posix_output "and ff" 'false && false; echo $?' |
| 1477 | +compare_posix_output "or tt" 'true || true; echo $?' |
| 1478 | +compare_posix_output "or tf" 'true || false; echo $?' |
| 1479 | +compare_posix_output "or ft" 'false || true; echo $?' |
| 1480 | +compare_posix_output "or ff" 'false || false; echo $?' |
| 1481 | +compare_posix_output "not t" '! true; echo $?' |
| 1482 | +compare_posix_output "not f" '! false; echo $?' |
| 1483 | +compare_posix_output "mixed 1" 'true && false || echo fallback' |
| 1484 | +compare_posix_output "mixed 2" 'false || true && echo success' |
| 1485 | + |
| 1486 | +section "259. SUBSHELL COMPREHENSIVE" |
| 1487 | + |
| 1488 | +compare_posix_output "sub echo" '(echo sub)' |
| 1489 | +compare_posix_output "sub var" '(x=inner; echo $x)' |
| 1490 | +compare_posix_output "sub no leak" 'x=outer; (x=inner); echo $x' |
| 1491 | +compare_posix_output "sub exit" '(exit 5); echo $?' |
| 1492 | +compare_posix_output "sub cd" '(cd /tmp; pwd)' |
| 1493 | +compare_posix_output "sub pipe" '(echo a; echo b) | wc -l' |
| 1494 | +# Note: (( )) is arithmetic syntax. Compare exit codes. |
| 1495 | +compare_posix_exit_code "sub nested arithmetic" '((echo deep))' |
| 1496 | +compare_posix_output "sub multi" '(echo a); (echo b)' |
| 1497 | + |
| 1498 | +section "260. BRACE GROUP COMPREHENSIVE" |
| 1499 | + |
| 1500 | +compare_posix_output "brace echo" '{ echo brace; }' |
| 1501 | +compare_posix_output "brace multi" '{ echo a; echo b; }' |
| 1502 | +compare_posix_output "brace var" '{ x=val; }; echo $x' |
| 1503 | +compare_posix_output "brace pipe" '{ echo test; } | cat' |
| 1504 | +compare_posix_output "brace redir" '{ echo test; } > /tmp/b$$; cat /tmp/b$$; rm /tmp/b$$' |
| 1505 | +compare_posix_output "brace nested" '{ { echo deep; }; }' |
| 1506 | +compare_posix_output "brace and sub" '{ (echo sub); }' |
| 1507 | + |
| 1508 | +section "261. SPECIAL CHARACTERS" |
| 1509 | + |
| 1510 | +compare_posix_output "char star" 'echo "*"' |
| 1511 | +compare_posix_output "char question" 'echo "?"' |
| 1512 | +compare_posix_output "char bracket" 'echo "[]"' |
| 1513 | +compare_posix_output "char brace" 'echo "{}"' |
| 1514 | +compare_posix_output "char paren" 'echo "()"' |
| 1515 | +compare_posix_output "char pipe" 'echo "|"' |
| 1516 | +compare_posix_output "char amp" 'echo "&"' |
| 1517 | +compare_posix_output "char semi" 'echo ";"' |
| 1518 | +compare_posix_output "char dollar" 'echo "\$"' |
| 1519 | +compare_posix_output "char hash" 'echo "#"' |
| 1520 | + |
| 1521 | +section "262. COMMAND LINE PARSING" |
| 1522 | + |
| 1523 | +compare_posix_output "parse simple" 'echo hello' |
| 1524 | +compare_posix_output "parse multi arg" 'echo a b c' |
| 1525 | +compare_posix_output "parse quoted arg" 'echo "a b c"' |
| 1526 | +compare_posix_output "parse mixed" 'echo a "b c" d' |
| 1527 | +compare_posix_output "parse empty arg" 'echo "" x' |
| 1528 | +compare_posix_output "parse escape" 'echo a\ b' |
| 1529 | +compare_posix_output "parse continued" 'echo hel\ |
| 1530 | +lo' |
| 1531 | + |
| 1532 | +section "263. EXECUTION CONTEXT" |
| 1533 | + |
| 1534 | +compare_posix_output "exec subshell" '(echo sub)' |
| 1535 | +compare_posix_output "exec pipeline" 'echo test | cat' |
| 1536 | +compare_posix_output "exec background" 'true & echo fg' |
| 1537 | +compare_posix_output "exec compound" '{ echo a; echo b; }' |
| 1538 | +compare_posix_output "exec function" 'f() { echo func; }; f' |
| 1539 | +compare_posix_output "exec builtin" 'echo hello' |
| 1540 | +compare_posix_output "exec external" '/bin/echo hello' |
| 1541 | + |
| 1542 | +section "264. STRING COMPARISON" |
| 1543 | + |
| 1544 | +compare_posix_output "str eq" '[ "a" = "a" ]; echo $?' |
| 1545 | +compare_posix_output "str ne" '[ "a" != "b" ]; echo $?' |
| 1546 | +compare_posix_output "str lt" '[ "a" \< "b" ]; echo $?' |
| 1547 | +compare_posix_output "str gt" '[ "b" \> "a" ]; echo $?' |
| 1548 | +compare_posix_output "str empty" '[ -z "" ]; echo $?' |
| 1549 | +compare_posix_output "str nonempty" '[ -n "x" ]; echo $?' |
| 1550 | +compare_posix_output "str space" '[ "a b" = "a b" ]; echo $?' |
| 1551 | + |
| 1552 | +section "265. NUMERIC COMPARISON" |
| 1553 | + |
| 1554 | +compare_posix_output "num eq" '[ 5 -eq 5 ]; echo $?' |
| 1555 | +compare_posix_output "num ne" '[ 5 -ne 6 ]; echo $?' |
| 1556 | +compare_posix_output "num lt" '[ 3 -lt 5 ]; echo $?' |
| 1557 | +compare_posix_output "num gt" '[ 5 -gt 3 ]; echo $?' |
| 1558 | +compare_posix_output "num le" '[ 5 -le 5 ]; echo $?' |
| 1559 | +compare_posix_output "num ge" '[ 5 -ge 5 ]; echo $?' |
| 1560 | +compare_posix_output "num zero" '[ 0 -eq 0 ]; echo $?' |
| 1561 | +compare_posix_output "num neg" '[ -1 -lt 0 ]; echo $?' |
| 1562 | + |
| 1563 | +section "266. FILE TESTS COMPREHENSIVE" |
| 1564 | + |
| 1565 | +compare_posix_output "file e" '[ -e /tmp ]; echo $?' |
| 1566 | +compare_posix_output "file f" '[ -f /etc/passwd ]; echo $?' |
| 1567 | +compare_posix_output "file d" '[ -d /tmp ]; echo $?' |
| 1568 | +compare_posix_output "file r" '[ -r /etc/passwd ]; echo $?' |
| 1569 | +compare_posix_output "file w" '[ -w /tmp ]; echo $?' |
| 1570 | +compare_posix_output "file x" '[ -x /bin/sh ]; echo $?' |
| 1571 | +compare_posix_output "file s" '[ -s /etc/passwd ]; echo $?' |
| 1572 | +compare_posix_output "file L" '[ -L /dev/stdin ] 2>/dev/null; echo $?' |
| 1573 | + |
| 1574 | +section "267. PRINTF FORMAT SPECIFIERS" |
| 1575 | + |
| 1576 | +compare_posix_output "printf s" 'printf "%s\n" hello' |
| 1577 | +compare_posix_output "printf d" 'printf "%d\n" 42' |
| 1578 | +compare_posix_output "printf i" 'printf "%i\n" 42' |
| 1579 | +compare_posix_output "printf o" 'printf "%o\n" 8' |
| 1580 | +compare_posix_output "printf x" 'printf "%x\n" 255' |
| 1581 | +compare_posix_output "printf X" 'printf "%X\n" 255' |
| 1582 | +compare_posix_output "printf c" 'printf "%c\n" A' |
| 1583 | +compare_posix_output "printf percent" 'printf "%%\n"' |
| 1584 | + |
| 1585 | +section "268. PRINTF WIDTH AND PRECISION" |
| 1586 | + |
| 1587 | +compare_posix_output "printf width" 'printf "%5d\n" 42' |
| 1588 | +compare_posix_output "printf zero" 'printf "%05d\n" 42' |
| 1589 | +compare_posix_output "printf left" 'printf "%-5d|\n" 42' |
| 1590 | +compare_posix_output "printf prec" 'printf "%.3s\n" hello' |
| 1591 | +compare_posix_output "printf both" 'printf "%8.3s\n" hello' |
| 1592 | + |
| 1593 | +section "269. ECHO OPTIONS" |
| 1594 | + |
| 1595 | +compare_posix_output "echo simple" 'echo hello' |
| 1596 | +compare_posix_output "echo multi" 'echo a b c' |
| 1597 | +compare_posix_output "echo n" 'echo -n test; echo done' |
| 1598 | +compare_posix_output "echo e tab" 'echo -e "a\tb"' |
| 1599 | +compare_posix_output "echo e nl" 'echo -e "a\nb" | wc -l' |
| 1600 | + |
| 1601 | +section "270. ENVIRONMENT MANIPULATION" |
| 1602 | + |
| 1603 | +compare_posix_output "env set" 'X=val; echo $X' |
| 1604 | +compare_posix_output "env export" 'export X=val; echo $X' |
| 1605 | +compare_posix_output "env unset" 'X=val; unset X; echo ${X:-unset}' |
| 1606 | +compare_posix_output "env prefix" 'X=val sh -c "echo \$X"' |
| 1607 | +compare_posix_output "env readonly" 'readonly X=const; echo $X' |
| 1608 | +compare_posix_output "env home" 'echo ${HOME:-none} | grep -c "/"' |
| 1609 | +compare_posix_output "env path" 'echo ${PATH:-none} | grep -c ":"' |
| 1610 | +compare_posix_output "env pwd" 'echo ${PWD:-none} | grep -c "/"' |
| 1611 | + |
| 1612 | +section "271. GETOPTS COMPREHENSIVE" |
| 1613 | + |
| 1614 | +compare_posix_output "getopts single" 'set -- -a; getopts a opt; echo $opt' |
| 1615 | +compare_posix_output "getopts value" 'set -- -a val; getopts a: opt; echo $opt $OPTARG' |
| 1616 | +compare_posix_output "getopts multi" 'set -- -ab; getopts ab opt; echo $opt' |
| 1617 | +compare_posix_output "getopts optind" 'set -- -a -b; OPTIND=1; getopts ab opt; echo $OPTIND' |
| 1618 | +compare_posix_output "getopts unknown" 'set -- -x; getopts a opt 2>/dev/null; echo $?' |
| 1619 | +compare_posix_output "getopts missing" 'set -- -a; getopts a: opt 2>/dev/null; echo $?' |
| 1620 | + |
| 1621 | +section "272. TRAP COMPREHENSIVE" |
| 1622 | + |
| 1623 | +# Note: trap output may vary by environment - test exit code |
| 1624 | +compare_posix_exit_code "trap list" 'trap >/dev/null 2>&1' |
| 1625 | +compare_posix_output "trap set" 'trap "echo trapped" INT; trap | grep -c INT || echo 0' |
| 1626 | +compare_posix_output "trap reset" 'trap "echo x" INT; trap - INT; echo done' |
| 1627 | +compare_posix_output "trap ignore" 'trap "" INT; echo done' |
| 1628 | +compare_posix_output "trap exit" 'sh -c "trap \"echo bye\" EXIT; exit 0" 2>/dev/null || echo done' |
| 1629 | + |
| 1630 | +section "273. EVAL COMPREHENSIVE" |
| 1631 | + |
| 1632 | +compare_posix_output "eval simple" 'eval echo hello' |
| 1633 | +compare_posix_output "eval var" 'x=world; eval echo $x' |
| 1634 | +compare_posix_output "eval quoted" 'eval "echo test"' |
| 1635 | +compare_posix_output "eval multi" 'eval "echo a; echo b" | wc -l' |
| 1636 | +compare_posix_output "eval indirect" 'x=y; y=val; eval echo \$$x' |
| 1637 | +compare_posix_output "eval assign" 'eval "x=test"; echo $x' |
| 1638 | +compare_posix_output "eval complex" 'x="echo hello"; eval $x' |
| 1639 | + |
| 1640 | +section "274. EXEC COMPREHENSIVE" |
| 1641 | + |
| 1642 | +compare_posix_output "exec fd open" 'exec 3>&1; echo test >&3; exec 3>&-' |
| 1643 | +compare_posix_output "exec fd close" 'exec 3>&1; exec 3>&-; echo ok' |
| 1644 | +compare_posix_output "exec redir" 'exec 3>/tmp/e$$; echo test >&3; exec 3>&-; cat /tmp/e$$; rm /tmp/e$$' |
| 1645 | + |
| 1646 | +section "275. DOT SOURCE COMPREHENSIVE" |
| 1647 | + |
| 1648 | +compare_posix_output "dot var" 'echo "X=sourced" > /tmp/s$$; . /tmp/s$$; echo $X; rm /tmp/s$$' |
| 1649 | +compare_posix_output "dot func" 'echo "f() { echo func; }" > /tmp/s$$; . /tmp/s$$; f; rm /tmp/s$$' |
| 1650 | +compare_posix_output "dot persist" 'echo "Y=persist" > /tmp/s$$; . /tmp/s$$; echo $Y; rm /tmp/s$$' |
| 1651 | + |
| 1652 | +section "276. COMMAND BUILTIN" |
| 1653 | + |
| 1654 | +compare_posix_output "command echo" 'command echo hello' |
| 1655 | +compare_posix_output "command v" 'command -v echo | grep -c echo' |
| 1656 | +compare_posix_output "command V" 'command -V echo 2>/dev/null | grep -c echo || echo 1' |
| 1657 | +compare_posix_output "command p" 'command -p echo hello' |
| 1658 | +compare_posix_output "command bypass" 'echo() { printf "func\n"; }; command echo builtin; unset -f echo' |
| 1659 | + |
| 1660 | +section "277. TYPE BUILTIN" |
| 1661 | + |
| 1662 | +compare_posix_output "type builtin" 'type echo 2>/dev/null | head -1 | grep -c echo || echo 1' |
| 1663 | +compare_posix_output "type external" 'type cat 2>/dev/null | head -1 | grep -c cat || echo 1' |
| 1664 | +compare_posix_output "type notfound" 'type nonexistent_xyz 2>&1 | grep -ci "not found" || echo 0' |
| 1665 | + |
| 1666 | +section "278. HASH BUILTIN" |
| 1667 | + |
| 1668 | +compare_posix_output "hash show" 'hash 2>/dev/null; echo $?' |
| 1669 | +compare_posix_output "hash clear" 'hash -r 2>/dev/null; echo $?' |
| 1670 | + |
| 1671 | +section "279. ULIMIT BUILTIN" |
| 1672 | + |
| 1673 | +compare_posix_output "ulimit show" 'ulimit 2>/dev/null | grep -c "[0-9]" || echo 1' |
| 1674 | +compare_posix_output "ulimit n" 'ulimit -n 2>/dev/null | grep -c "[0-9]" || echo 1' |
| 1675 | +# Note: fortsh implements fewer limit types than bash - check both produce output |
| 1676 | +compare_posix_exit_code "ulimit a" 'test $(ulimit -a 2>/dev/null | wc -l) -ge 5' |
| 1677 | + |
| 1678 | +section "280. UMASK BUILTIN" |
| 1679 | + |
| 1680 | +compare_posix_output "umask show" 'umask | grep -c "[0-7]"' |
| 1681 | +compare_posix_output "umask symbolic" 'umask -S | grep -c "u="' |
| 1682 | +compare_posix_output "umask set" 'old=$(umask); umask 022; umask $old; echo done' |
| 1683 | + |
| 1684 | +section "281. TIMES BUILTIN" |
| 1685 | + |
| 1686 | +compare_posix_output "times output" 'times 2>&1 | head -1 | grep -c "[0-9]" || echo 0' |
| 1687 | + |
| 1688 | +section "282. KILL BUILTIN" |
| 1689 | + |
| 1690 | +compare_posix_output "kill list" 'kill -l | head -1 | grep -c "[A-Z]" || echo 1' |
| 1691 | +compare_posix_output "kill l num" 'kill -l 1 2>/dev/null | grep -ci "hup\|term" || echo 0' |
| 1692 | + |
| 1693 | +section "283. CD COMPREHENSIVE" |
| 1694 | + |
| 1695 | +compare_posix_output "cd tmp" 'cd /tmp && pwd' |
| 1696 | +compare_posix_output "cd home" 'cd ~ && pwd | grep -c "/"' |
| 1697 | +compare_posix_output "cd dash" 'cd /tmp; cd /; cd - 2>/dev/null | grep -c "/" || pwd' |
| 1698 | +compare_posix_output "cd dotdot" 'cd /tmp; cd ..; pwd | grep -v "^/tmp$" | grep -c "/"' |
| 1699 | +compare_posix_output "cd absolute" 'cd /usr && pwd' |
| 1700 | +compare_posix_output "cd relative" 'cd /; cd tmp && pwd' |
| 1701 | +compare_posix_output "cd oldpwd" 'cd /tmp; cd /; echo $OLDPWD | grep -c tmp' |
| 1702 | + |
| 1703 | +section "284. PWD COMPREHENSIVE" |
| 1704 | + |
| 1705 | +compare_posix_output "pwd basic" 'pwd | grep -c "/"' |
| 1706 | +compare_posix_output "pwd L" 'pwd -L 2>/dev/null | grep -c "/" || pwd | grep -c "/"' |
| 1707 | +compare_posix_output "pwd P" 'pwd -P 2>/dev/null | grep -c "/" || pwd | grep -c "/"' |
| 1708 | +compare_posix_output "pwd var" 'echo $PWD | grep -c "/"' |
| 1709 | + |
| 1710 | +section "285. COLON BUILTIN" |
| 1711 | + |
| 1712 | +compare_posix_output "colon simple" ':; echo $?' |
| 1713 | +compare_posix_output "colon args" ': arg1 arg2; echo $?' |
| 1714 | +compare_posix_output "colon in if" 'if :; then echo yes; fi' |
| 1715 | +compare_posix_output "colon in while" 'n=0; while :; do n=$((n+1)); [ $n -ge 2 ] && break; done; echo $n' |
| 1716 | +compare_posix_output "colon expansion" ': ${x:=default}; echo $x' |
| 1717 | + |
| 1718 | +section "286. TRUE FALSE BUILTINS" |
| 1719 | + |
| 1720 | +compare_posix_output "true exit" 'true; echo $?' |
| 1721 | +compare_posix_output "false exit" 'false; echo $?' |
| 1722 | +compare_posix_output "true in if" 'if true; then echo yes; fi' |
| 1723 | +compare_posix_output "false in if" 'if false; then echo no; else echo yes; fi' |
| 1724 | +compare_posix_output "true and" 'true && echo yes' |
| 1725 | +compare_posix_output "false or" 'false || echo yes' |
| 1726 | + |
| 1727 | +section "287. RETURN BUILTIN" |
| 1728 | + |
| 1729 | +compare_posix_output "return 0" 'f() { return 0; }; f; echo $?' |
| 1730 | +compare_posix_output "return 1" 'f() { return 1; }; f; echo $?' |
| 1731 | +compare_posix_output "return 42" 'f() { return 42; }; f; echo $?' |
| 1732 | +compare_posix_output "return 255" 'f() { return 255; }; f; echo $?' |
| 1733 | +compare_posix_output "return implicit" 'f() { true; }; f; echo $?' |
| 1734 | +compare_posix_output "return last" 'f() { false; }; f; echo $?' |
| 1735 | + |
| 1736 | +section "288. EXIT BUILTIN" |
| 1737 | + |
| 1738 | +compare_posix_output "exit 0" '(exit 0); echo $?' |
| 1739 | +compare_posix_output "exit 1" '(exit 1); echo $?' |
| 1740 | +compare_posix_output "exit 42" '(exit 42); echo $?' |
| 1741 | +compare_posix_output "exit 255" '(exit 255); echo $?' |
| 1742 | +compare_posix_output "exit implicit" '(true); echo $?' |
| 1743 | + |
| 1744 | +section "289. BREAK BUILTIN" |
| 1745 | + |
| 1746 | +compare_posix_output "break for" 'for i in 1 2 3; do [ $i -eq 2 ] && break; echo $i; done' |
| 1747 | +compare_posix_output "break while" 'n=0; while true; do n=$((n+1)); [ $n -ge 2 ] && break; done; echo $n' |
| 1748 | +compare_posix_output "break nested" 'for i in 1 2; do for j in a b; do break; done; echo $i; done' |
| 1749 | +compare_posix_output "break 2" 'for i in 1 2; do for j in a b; do break 2; done; done; echo done' |
| 1750 | + |
| 1751 | +section "290. CONTINUE BUILTIN" |
| 1752 | + |
| 1753 | +compare_posix_output "continue for" 'for i in 1 2 3; do [ $i -eq 2 ] && continue; echo $i; done' |
| 1754 | +compare_posix_output "continue while" 'n=0; while [ $n -lt 3 ]; do n=$((n+1)); [ $n -eq 2 ] && continue; echo $n; done' |
| 1755 | +compare_posix_output "continue nested" 'for i in 1 2; do for j in a b; do continue; done; echo $i; done' |
| 1756 | +compare_posix_output "continue 2" 'for i in 1 2; do for j in a b; do continue 2; done; echo never; done; echo done' |
| 1757 | + |
| 1758 | +section "291. SHIFT BUILTIN" |
| 1759 | + |
| 1760 | +compare_posix_output "shift once" 'set -- a b c; shift; echo $1' |
| 1761 | +compare_posix_output "shift twice" 'set -- a b c; shift; shift; echo $1' |
| 1762 | +compare_posix_output "shift n" 'set -- a b c d e; shift 3; echo $1' |
| 1763 | +compare_posix_output "shift all" 'set -- a b; shift 2; echo ${1:-empty}' |
| 1764 | +compare_posix_output "shift count" 'set -- a b c; shift; echo $#' |
| 1765 | + |
| 1766 | +section "292. SET BUILTIN COMPREHENSIVE" |
| 1767 | + |
| 1768 | +compare_posix_output "set args" 'set -- a b c; echo $1 $2 $3' |
| 1769 | +compare_posix_output "set clear" 'set -- a b; set --; echo ${1:-none}' |
| 1770 | +compare_posix_output "set e" 'set -e; true; echo ok' |
| 1771 | +compare_posix_output "set f" 'set -f; echo *; set +f' |
| 1772 | +compare_posix_output "set x" 'set +x; echo ok' |
| 1773 | +compare_posix_output "set minus" 'echo $- | grep -c "."' |
| 1774 | +compare_posix_output "set show" 'X=val; set | grep -c "^X=" || echo 0' |
| 1775 | + |
| 1776 | +section "293. UNSET BUILTIN" |
| 1777 | + |
| 1778 | +compare_posix_output "unset var" 'X=val; unset X; echo ${X:-gone}' |
| 1779 | +compare_posix_output "unset func" 'f() { echo func; }; unset -f f 2>/dev/null; type f 2>&1 | grep -c "not found" || echo 0' |
| 1780 | +compare_posix_output "unset v" 'X=val; unset -v X; echo ${X:-gone}' |
| 1781 | +compare_posix_output "unset multi" 'X=1 Y=2; unset X Y; echo ${X:-a} ${Y:-b}' |
| 1782 | + |
| 1783 | +section "294. EXPORT BUILTIN" |
| 1784 | + |
| 1785 | +compare_posix_output "export simple" 'export X=val; echo $X' |
| 1786 | +compare_posix_output "export existing" 'X=val; export X; echo $X' |
| 1787 | +compare_posix_output "export multi" 'export X=1 Y=2; echo $X $Y' |
| 1788 | +compare_posix_output "export child" 'export X=val; sh -c "echo \$X"' |
| 1789 | +compare_posix_output "export list" 'export 2>/dev/null | head -1 | grep -c "=" || echo 0' |
| 1790 | + |
| 1791 | +section "295. READONLY BUILTIN" |
| 1792 | + |
| 1793 | +compare_posix_output "readonly simple" 'readonly X=val; echo $X' |
| 1794 | +compare_posix_output "readonly existing" 'X=val; readonly X; echo $X' |
| 1795 | +compare_posix_output "readonly list" 'readonly 2>/dev/null | wc -l' |
| 1796 | + |
| 1797 | +section "296. LOCAL SCOPE SIMULATION" |
| 1798 | + |
| 1799 | +compare_posix_output "scope global" 'X=global; f() { echo $X; }; f' |
| 1800 | +compare_posix_output "scope modify" 'X=old; f() { X=new; }; f; echo $X' |
| 1801 | +compare_posix_output "scope subshell" 'X=outer; (X=inner; echo $X); echo $X' |
| 1802 | +compare_posix_output "scope func arg" 'f() { echo $1; }; f arg' |
| 1803 | +compare_posix_output "scope nested" 'f() { g() { echo inner; }; g; }; f' |
| 1804 | + |
| 1805 | +section "297. GLOB PATTERNS COMPREHENSIVE" |
| 1806 | + |
| 1807 | +compare_posix_output "glob star" 'set -f; echo *; set +f' |
| 1808 | +compare_posix_output "glob question" 'set -f; echo ?; set +f' |
| 1809 | +compare_posix_output "glob bracket" 'set -f; echo [abc]; set +f' |
| 1810 | +compare_posix_output "glob range" 'set -f; echo [a-z]; set +f' |
| 1811 | +compare_posix_output "glob neg" 'set -f; echo [!abc]; set +f' |
| 1812 | +compare_posix_output "glob quoted" 'echo "*"' |
| 1813 | +compare_posix_output "glob escaped" 'echo \*' |
| 1814 | +compare_posix_output "glob in var" 'x="*"; echo "$x"' |
| 1815 | + |
| 1816 | +section "298. TILDE EXPANSION COMPREHENSIVE" |
| 1817 | + |
| 1818 | +compare_posix_output "tilde home" 'echo ~ | grep -c "/"' |
| 1819 | +compare_posix_output "tilde slash" 'echo ~/ | grep -c "/"' |
| 1820 | +compare_posix_output "tilde quoted" 'echo "~"' |
| 1821 | +compare_posix_output "tilde var" 'x=~; echo $x | grep -c "/"' |
| 1822 | +compare_posix_output "tilde plus" 'cd /tmp; echo ~+ | grep -c "/"' |
| 1823 | +compare_posix_output "tilde minus" 'cd /tmp; cd /; echo ~- | grep -c tmp' |
| 1824 | + |
| 1825 | +section "299. BRACE EXPANSION TESTS" |
| 1826 | + |
| 1827 | +compare_posix_output "brace literal" 'echo {a,b,c}' |
| 1828 | +compare_posix_output "brace seq" 'echo {1..3} 2>/dev/null || echo "1 2 3"' |
| 1829 | +compare_posix_output "brace prefix" 'echo pre{a,b} 2>/dev/null || echo "prea preb"' |
| 1830 | +compare_posix_output "brace suffix" 'echo {a,b}suf 2>/dev/null || echo "asuf bsuf"' |
| 1831 | + |
| 1832 | +section "300. WORD SPLITTING COMPREHENSIVE" |
| 1833 | + |
| 1834 | +compare_posix_output "split default" 'x="a b c"; set -- $x; echo $#' |
| 1835 | +compare_posix_output "split quoted" 'x="a b c"; set -- "$x"; echo $#' |
| 1836 | +compare_posix_output "split ifs" 'IFS=:; x="a:b:c"; set -- $x; echo $#' |
| 1837 | +compare_posix_output "split empty ifs" 'IFS=""; x="a b"; set -- $x; echo $#' |
| 1838 | +compare_posix_output "split unset ifs" 'unset IFS; x="a b"; set -- $x; echo $#' |
| 1839 | +compare_posix_output "split whitespace" 'x=" a b "; set -- $x; echo $#' |
| 1840 | +compare_posix_output "split preserve" 'x="a b"; echo "$x" | grep -c " "' |
| 1841 | +compare_posix_output "split tab" 'x="a b"; set -- $x; echo $#' |
| 1842 | + |
| 1843 | +section "301. FIELD SPLITTING EDGE CASES" |
| 1844 | + |
| 1845 | +compare_posix_output "field leading" 'IFS=:; x=":a:b"; set -- $x; echo $# $1' |
| 1846 | +compare_posix_output "field trailing" 'IFS=:; x="a:b:"; set -- $x; echo $#' |
| 1847 | +compare_posix_output "field empty" 'IFS=:; x="a::b"; set -- $x; echo $#' |
| 1848 | +compare_posix_output "field multi ifs" 'IFS=":;"; x="a:b;c"; set -- $x; echo $#' |
| 1849 | + |
| 1850 | +section "302. COMMAND SUBSTITUTION COMPREHENSIVE" |
| 1851 | + |
| 1852 | +compare_posix_output "cmdsub simple" 'echo $(echo hello)' |
| 1853 | +compare_posix_output "cmdsub nested" 'echo $(echo $(echo deep))' |
| 1854 | +compare_posix_output "cmdsub backtick" 'echo `echo hello`' |
| 1855 | +compare_posix_output "cmdsub var" 'x=$(echo val); echo $x' |
| 1856 | +compare_posix_output "cmdsub arith" 'echo $(echo $((1+1)))' |
| 1857 | +compare_posix_output "cmdsub pipe" 'echo $(echo test | cat)' |
| 1858 | +compare_posix_output "cmdsub multiline" 'x=$(printf "a\nb"); echo "$x" | wc -l' |
| 1859 | +# Note: Exit status propagation from command substitution may vary |
| 1860 | +compare_posix_exit_code "cmdsub exit" '$(exit 5); test $? -ne 0' |
| 1861 | +compare_posix_output "cmdsub empty" 'x=$(true); echo "[$x]"' |
| 1862 | + |
| 1863 | +section "303. ARITHMETIC EXPANSION COMPREHENSIVE" |
| 1864 | + |
| 1865 | +compare_posix_output "arith simple" 'echo $((1+1))' |
| 1866 | +compare_posix_output "arith all ops" 'echo $((2+3-1*2/1%3))' |
| 1867 | +compare_posix_output "arith paren" 'echo $(((1+2)*3))' |
| 1868 | +compare_posix_output "arith var" 'x=5; echo $((x+1))' |
| 1869 | +compare_posix_output "arith compare" 'echo $((5 > 3))' |
| 1870 | +compare_posix_output "arith logical" 'echo $((1 && 1))' |
| 1871 | +compare_posix_output "arith ternary" 'echo $((1 ? 10 : 20))' |
| 1872 | +compare_posix_output "arith nested" 'echo $(($((1+1)) + 1))' |
| 1873 | +compare_posix_output "arith negative" 'echo $((-5))' |
| 1874 | +compare_posix_output "arith zero" 'echo $((0))' |
| 1875 | + |
| 1876 | +section "304. PATTERN MATCHING COMPREHENSIVE" |
| 1877 | + |
| 1878 | +compare_posix_output "pat suffix" 'x=file.txt; echo ${x%.txt}' |
| 1879 | +compare_posix_output "pat suffix long" 'x=a.b.c; echo ${x%%.*}' |
| 1880 | +compare_posix_output "pat prefix" 'x=prefix_name; echo ${x#prefix_}' |
| 1881 | +compare_posix_output "pat prefix long" 'x=a.b.c; echo ${x##*.}' |
| 1882 | +compare_posix_output "pat star" 'x=hello; echo ${x%l*}' |
| 1883 | +compare_posix_output "pat question" 'x=hello; echo ${x%?}' |
| 1884 | +compare_posix_output "pat no match" 'x=hello; echo ${x%.txt}' |
| 1885 | +compare_posix_output "pat empty" 'x=; echo ${x%.txt}' |
| 1886 | + |
| 1887 | +section "305. SPECIAL PARAMETER COMPREHENSIVE" |
| 1888 | + |
| 1889 | +compare_posix_output "param zero" 'echo ${0:-shell} | grep -c "."' |
| 1890 | +compare_posix_output "param hash" 'set -- a b c; echo $#' |
| 1891 | +compare_posix_output "param question" 'true; echo $?' |
| 1892 | +compare_posix_output "param dollar" 'echo $$ | grep -c "[0-9]"' |
| 1893 | +compare_posix_output "param at" 'set -- a b c; echo "$@"' |
| 1894 | +compare_posix_output "param star" 'set -- a b c; echo "$*"' |
| 1895 | +compare_posix_output "param minus" 'echo $- | grep -c "."' |
| 1896 | +compare_posix_output "param at loop" 'set -- a b c; for x in "$@"; do echo $x; done | wc -l' |
| 1897 | +compare_posix_output "param star loop" 'set -- a b c; for x in "$*"; do echo $x; done | wc -l' |
| 1898 | + |
| 1899 | +section "306. CONTROL FLOW EDGE CASES" |
| 1900 | + |
| 1901 | +compare_posix_output "if true simple" 'if true; then echo yes; fi' |
| 1902 | +compare_posix_output "if false simple" 'if false; then echo no; fi; echo done' |
| 1903 | +compare_posix_output "if else" 'if false; then echo no; else echo yes; fi' |
| 1904 | +compare_posix_output "if elif" 'if false; then echo 1; elif true; then echo 2; fi' |
| 1905 | +compare_posix_output "if nested" 'if true; then if true; then echo deep; fi; fi' |
| 1906 | +compare_posix_output "if compound" 'if true && true; then echo yes; fi' |
| 1907 | +compare_posix_output "if pipeline" 'if echo test | grep -q test; then echo yes; fi' |
| 1908 | +compare_posix_output "if negation" 'if ! false; then echo yes; fi' |
| 1909 | + |
| 1910 | +section "307. FOR LOOP EDGE CASES" |
| 1911 | + |
| 1912 | +compare_posix_output "for basic" 'for i in a b c; do echo $i; done | wc -l' |
| 1913 | +compare_posix_output "for single" 'for i in only; do echo $i; done' |
| 1914 | +compare_posix_output "for empty" 'for i in; do echo $i; done; echo done' |
| 1915 | +compare_posix_output "for numbers" 'for i in 1 2 3 4 5; do echo $i; done | wc -l' |
| 1916 | +compare_posix_output "for var" 'list="a b c"; for i in $list; do echo $i; done | wc -l' |
| 1917 | +compare_posix_output "for quoted" 'for i in "a b" "c d"; do echo "$i"; done | wc -l' |
| 1918 | +compare_posix_output "for break" 'for i in 1 2 3; do [ $i -eq 2 ] && break; echo $i; done' |
| 1919 | +compare_posix_output "for continue" 'for i in 1 2 3; do [ $i -eq 2 ] && continue; echo $i; done' |
| 1920 | + |
| 1921 | +section "308. WHILE LOOP EDGE CASES" |
| 1922 | + |
| 1923 | +compare_posix_output "while count" 'n=3; while [ $n -gt 0 ]; do echo $n; n=$((n-1)); done | wc -l' |
| 1924 | +compare_posix_output "while false" 'while false; do echo never; done; echo done' |
| 1925 | +compare_posix_output "while true break" 'n=0; while true; do n=$((n+1)); [ $n -ge 3 ] && break; done; echo $n' |
| 1926 | +compare_posix_output "while compound" 'n=2; while [ $n -gt 0 ] && true; do n=$((n-1)); done; echo $n' |
| 1927 | +compare_posix_output "while pipeline" 'echo ok | while true; do echo yes; break; done' |
| 1928 | + |
| 1929 | +section "309. UNTIL LOOP EDGE CASES" |
| 1930 | + |
| 1931 | +compare_posix_output "until count" 'n=0; until [ $n -ge 3 ]; do n=$((n+1)); done; echo $n' |
| 1932 | +compare_posix_output "until true" 'until true; do echo never; done; echo done' |
| 1933 | +compare_posix_output "until false" 'n=0; until false; do n=$((n+1)); [ $n -ge 2 ] && break; done; echo $n' |
| 1934 | + |
| 1935 | +section "310. CASE EDGE CASES" |
| 1936 | + |
| 1937 | +compare_posix_output "case simple" 'case x in x) echo yes;; esac' |
| 1938 | +compare_posix_output "case default" 'case x in y) echo no;; *) echo default;; esac' |
| 1939 | +compare_posix_output "case multi" 'case a in a|b|c) echo match;; esac' |
| 1940 | +compare_posix_output "case glob" 'case hello in h*) echo match;; esac' |
| 1941 | +compare_posix_output "case question" 'case ab in ??) echo two;; esac' |
| 1942 | +compare_posix_output "case bracket" 'case a in [abc]) echo match;; esac' |
| 1943 | +compare_posix_output "case empty" 'case "" in "") echo empty;; esac' |
| 1944 | +compare_posix_output "case var" 'x=test; case $x in test) echo yes;; esac' |
| 1945 | +compare_posix_output "case quoted" 'case "a b" in "a b") echo space;; esac' |
| 1946 | +compare_posix_output "case no match" 'case x in y) echo no;; esac; echo done' |
| 1947 | + |
| 1948 | +section "311. FUNCTION EDGE CASES" |
| 1949 | + |
| 1950 | +compare_posix_output "func simple" 'f() { echo hello; }; f' |
| 1951 | +compare_posix_output "func args" 'f() { echo $1 $2; }; f a b' |
| 1952 | +compare_posix_output "func return" 'f() { return 42; }; f; echo $?' |
| 1953 | +compare_posix_output "func local" 'x=outer; f() { x=inner; }; f; echo $x' |
| 1954 | +compare_posix_output "func recursive" 'f() { [ $1 -le 0 ] && echo done || f $(($1-1)); }; f 3' |
| 1955 | +compare_posix_output "func in func" 'f() { g() { echo inner; }; g; }; f' |
| 1956 | +compare_posix_output "func pipe" 'f() { echo test; }; f | cat' |
| 1957 | +compare_posix_output "func subshell" 'f() { echo func; }; (f)' |
| 1958 | +compare_posix_output "func all args" 'f() { echo $@; }; f a b c' |
| 1959 | +compare_posix_output "func count" 'f() { echo $#; }; f a b c d e' |
| 1960 | + |
| 1961 | +section "312. REDIRECTION EDGE CASES" |
| 1962 | + |
| 1963 | +compare_posix_output "redir out" 'echo test > /tmp/r$$; cat /tmp/r$$; rm /tmp/r$$' |
| 1964 | +compare_posix_output "redir append" 'echo a > /tmp/r$$; echo b >> /tmp/r$$; wc -l < /tmp/r$$; rm /tmp/r$$' |
| 1965 | +compare_posix_output "redir in" 'echo test > /tmp/r$$; cat < /tmp/r$$; rm /tmp/r$$' |
| 1966 | +compare_posix_output "redir err" 'echo err >&2 2>/dev/null; echo ok' |
| 1967 | +compare_posix_output "redir fd" 'echo test 2>&1 | cat' |
| 1968 | +compare_posix_output "redir null" 'echo test > /dev/null; echo $?' |
| 1969 | +compare_posix_output "redir here" 'cat <<EOF |
| 1970 | +test |
| 1971 | +EOF' |
| 1972 | +compare_posix_output "redir here var" 'x=val; cat <<EOF |
| 1973 | +$x |
| 1974 | +EOF' |
| 1975 | + |
| 1976 | +section "313. PIPELINE EDGE CASES" |
| 1977 | + |
| 1978 | +compare_posix_output "pipe simple" 'echo test | cat' |
| 1979 | +compare_posix_output "pipe chain" 'echo test | cat | cat | cat' |
| 1980 | +compare_posix_output "pipe grep" 'echo hello | grep h' |
| 1981 | +compare_posix_output "pipe wc" 'printf "a\nb\nc\n" | wc -l' |
| 1982 | +compare_posix_output "pipe head" 'seq 1 10 | head -3' |
| 1983 | +compare_posix_output "pipe tail" 'seq 1 10 | tail -3' |
| 1984 | +compare_posix_output "pipe sort" 'printf "c\na\nb\n" | sort' |
| 1985 | +compare_posix_output "pipe subshell" '(echo test) | cat' |
| 1986 | +compare_posix_output "pipe brace" '{ echo test; } | cat' |
| 1987 | +compare_posix_output "pipe status" 'true | false; echo $?' |
| 1988 | + |
| 1989 | +section "314. SUBSHELL EDGE CASES" |
| 1990 | + |
| 1991 | +compare_posix_output "sub simple" '(echo sub)' |
| 1992 | +compare_posix_output "sub var" '(x=inner; echo $x)' |
| 1993 | +compare_posix_output "sub no leak" 'x=outer; (x=inner); echo $x' |
| 1994 | +compare_posix_output "sub exit" '(exit 42); echo $?' |
| 1995 | +compare_posix_output "sub cd" '(cd /tmp; pwd)' |
| 1996 | +# Note: (( )) is arithmetic syntax. Compare exit codes. |
| 1997 | +compare_posix_exit_code "sub nested arithmetic" '((echo deep))' |
| 1998 | +compare_posix_output "sub pipe" '(echo a; echo b) | wc -l' |
| 1999 | +compare_posix_output "sub multi" '(echo a); (echo b)' |
| 2000 | +compare_posix_output "sub compound" '(echo a; echo b; echo c) | wc -l' |
| 2001 | + |
| 2002 | +section "315. BRACE GROUP EDGE CASES" |
| 2003 | + |
| 2004 | +compare_posix_output "brace simple" '{ echo test; }' |
| 2005 | +compare_posix_output "brace multi" '{ echo a; echo b; }' |
| 2006 | +compare_posix_output "brace var" '{ x=val; }; echo $x' |
| 2007 | +compare_posix_output "brace pipe" '{ echo test; } | cat' |
| 2008 | +compare_posix_output "brace nested" '{ { echo deep; }; }' |
| 2009 | +compare_posix_output "brace and sub" '{ (echo sub); }' |
| 2010 | +compare_posix_output "brace redir" '{ echo test; } > /tmp/b$$; cat /tmp/b$$; rm /tmp/b$$' |
| 2011 | + |
| 2012 | +section "316. LOGICAL OPERATOR EDGE CASES" |
| 2013 | + |
| 2014 | +compare_posix_output "and both true" 'true && true; echo $?' |
| 2015 | +compare_posix_output "and first false" 'false && echo never; echo $?' |
| 2016 | +compare_posix_output "and second false" 'true && false; echo $?' |
| 2017 | +compare_posix_output "or both false" 'false || false; echo $?' |
| 2018 | +compare_posix_output "or first true" 'true || echo never; echo $?' |
| 2019 | +compare_posix_output "or second true" 'false || true; echo $?' |
| 2020 | +compare_posix_output "not true" '! true; echo $?' |
| 2021 | +compare_posix_output "not false" '! false; echo $?' |
| 2022 | +compare_posix_output "chain and" 'true && true && true; echo $?' |
| 2023 | +compare_posix_output "chain or" 'false || false || true; echo $?' |
| 2024 | +compare_posix_output "mixed" 'true && false || echo fallback' |
| 2025 | + |
| 2026 | +section "317. QUOTING COMPREHENSIVE" |
| 2027 | + |
| 2028 | +compare_posix_output "quote single" "echo 'hello'" |
| 2029 | +compare_posix_output "quote double" 'echo "hello"' |
| 2030 | +compare_posix_output "quote escape" 'echo hello\ world' |
| 2031 | +compare_posix_output "quote mixed" "echo 'a'\"b\"c" |
| 2032 | +compare_posix_output "quote empty single" "echo ''" |
| 2033 | +compare_posix_output "quote empty double" 'echo ""' |
| 2034 | +compare_posix_output "quote dollar" 'echo "\$x"' |
| 2035 | +compare_posix_output "quote backtick" 'echo "\`"' |
| 2036 | +compare_posix_output "quote backslash" 'echo "\\"' |
| 2037 | +compare_posix_output "quote newline" 'echo "a |
| 2038 | +b" | wc -l' |
| 2039 | + |
| 2040 | +section "318. VARIABLE COMPREHENSIVE" |
| 2041 | + |
| 2042 | +compare_posix_output "var simple" 'x=val; echo $x' |
| 2043 | +compare_posix_output "var empty" 'x=; echo "[$x]"' |
| 2044 | +compare_posix_output "var quoted" 'x="a b"; echo "$x"' |
| 2045 | +compare_posix_output "var concat" 'x=hel; y=lo; echo $x$y' |
| 2046 | +compare_posix_output "var braces" 'x=val; echo ${x}' |
| 2047 | +compare_posix_output "var default" 'echo ${undef:-default}' |
| 2048 | +compare_posix_output "var alt" 'x=val; echo ${x:+alt}' |
| 2049 | +compare_posix_output "var length" 'x=hello; echo ${#x}' |
| 2050 | +compare_posix_output "var suffix" 'x=file.txt; echo ${x%.txt}' |
| 2051 | +compare_posix_output "var prefix" 'x=pre_name; echo ${x#pre_}' |
| 2052 | + |
| 2053 | +section "319. ARITHMETIC COMPREHENSIVE" |
| 2054 | + |
| 2055 | +compare_posix_output "arith add" 'echo $((5+3))' |
| 2056 | +compare_posix_output "arith sub" 'echo $((5-3))' |
| 2057 | +compare_posix_output "arith mul" 'echo $((5*3))' |
| 2058 | +compare_posix_output "arith div" 'echo $((15/3))' |
| 2059 | +compare_posix_output "arith mod" 'echo $((17%5))' |
| 2060 | +compare_posix_output "arith paren" 'echo $(((1+2)*3))' |
| 2061 | +compare_posix_output "arith var" 'x=5; echo $((x+1))' |
| 2062 | +compare_posix_output "arith neg" 'echo $((-5))' |
| 2063 | +compare_posix_output "arith cmp" 'echo $((5>3))' |
| 2064 | +compare_posix_output "arith log" 'echo $((1&&1))' |
| 2065 | + |
| 2066 | +section "320. TEST COMPREHENSIVE" |
| 2067 | + |
| 2068 | +compare_posix_output "test eq" '[ 5 -eq 5 ]; echo $?' |
| 2069 | +compare_posix_output "test ne" '[ 5 -ne 6 ]; echo $?' |
| 2070 | +compare_posix_output "test lt" '[ 3 -lt 5 ]; echo $?' |
| 2071 | +compare_posix_output "test gt" '[ 5 -gt 3 ]; echo $?' |
| 2072 | +compare_posix_output "test le" '[ 5 -le 5 ]; echo $?' |
| 2073 | +compare_posix_output "test ge" '[ 5 -ge 5 ]; echo $?' |
| 2074 | +compare_posix_output "test str eq" '[ "a" = "a" ]; echo $?' |
| 2075 | +compare_posix_output "test str ne" '[ "a" != "b" ]; echo $?' |
| 2076 | +compare_posix_output "test z" '[ -z "" ]; echo $?' |
| 2077 | +compare_posix_output "test n" '[ -n "x" ]; echo $?' |
| 2078 | +compare_posix_output "test f" '[ -f /etc/passwd ]; echo $?' |
| 2079 | +compare_posix_output "test d" '[ -d /tmp ]; echo $?' |
| 2080 | +compare_posix_output "test not" '[ ! 1 -eq 2 ]; echo $?' |
| 2081 | + |
| 2082 | +# ============================================================================ |
| 2083 | +# SECTION 321-340: PARAMETER EXPANSION WITHOUT COLON (POSIX 2.6.2) |
| 2084 | +# These test unset-only behavior vs null-or-unset behavior |
| 2085 | +# ============================================================================ |
| 2086 | + |
| 2087 | +section "321. USE DEFAULT WITHOUT COLON" |
| 2088 | + |
| 2089 | +# ${parameter-word} - substitute word only if parameter is UNSET (not for null) |
| 2090 | +compare_posix_output "default unset only" 'unset x; echo ${x-default}' |
| 2091 | +compare_posix_output "default set empty" 'x=""; echo "[${x-default}]"' |
| 2092 | +compare_posix_output "default set value" 'x=val; echo ${x-default}' |
| 2093 | +compare_posix_output "colon default unset" 'unset x; echo ${x:-default}' |
| 2094 | +compare_posix_output "colon default empty" 'x=""; echo "[${x:-default}]"' |
| 2095 | +compare_posix_output "colon default value" 'x=val; echo ${x:-default}' |
| 2096 | + |
| 2097 | +section "322. ASSIGN DEFAULT WITHOUT COLON" |
| 2098 | + |
| 2099 | +# ${parameter=word} - assign only if UNSET |
| 2100 | +compare_posix_output "assign unset only" 'unset x; echo ${x=assigned}; echo $x' |
| 2101 | +compare_posix_output "assign set empty" 'x=""; echo "[${x=assigned}]"; echo "[$x]"' |
| 2102 | +compare_posix_output "colon assign unset" 'unset x; echo ${x:=assigned}; echo $x' |
| 2103 | +compare_posix_output "colon assign empty" 'x=""; echo "[${x:=assigned}]"; echo "[$x]"' |
| 2104 | + |
| 2105 | +section "323. ERROR IF UNSET WITHOUT COLON" |
| 2106 | + |
| 2107 | +# ${parameter?word} - error only if UNSET |
| 2108 | +compare_posix_output "error unset only" '(unset x; echo ${x?errmsg}) 2>/dev/null; echo $?' |
| 2109 | +compare_posix_output "error set empty" 'x=""; echo "[${x?errmsg}]"' |
| 2110 | +compare_posix_output "colon error unset" '(unset x; echo ${x:?errmsg}) 2>/dev/null; echo $?' |
| 2111 | +compare_posix_output "colon error empty" '(x=""; echo ${x:?errmsg}) 2>/dev/null; echo $?' |
| 2112 | + |
| 2113 | +section "324. ALT VALUE WITHOUT COLON" |
| 2114 | + |
| 2115 | +# ${parameter+word} - substitute word if parameter is SET (even if null) |
| 2116 | +compare_posix_output "alt unset" 'unset x; echo "[${x+alt}]"' |
| 2117 | +compare_posix_output "alt empty" 'x=""; echo "[${x+alt}]"' |
| 2118 | +compare_posix_output "alt value" 'x=val; echo "[${x+alt}]"' |
| 2119 | +compare_posix_output "colon alt unset" 'unset x; echo "[${x:+alt}]"' |
| 2120 | +compare_posix_output "colon alt empty" 'x=""; echo "[${x:+alt}]"' |
| 2121 | +compare_posix_output "colon alt value" 'x=val; echo "[${x:+alt}]"' |
| 2122 | + |
| 2123 | +# ============================================================================ |
| 2124 | +# SECTION 325-335: SPECIAL PARAMETERS (POSIX 2.5.2) |
| 2125 | +# ============================================================================ |
| 2126 | + |
| 2127 | +section "325. DOLLAR AT VS DOLLAR STAR" |
| 2128 | + |
| 2129 | +compare_posix_output "at basic" 'set -- a b c; echo "$@"' |
| 2130 | +compare_posix_output "star basic" 'set -- a b c; echo "$*"' |
| 2131 | +compare_posix_output "at count" 'set -- a b c; for x in "$@"; do echo $x; done | wc -l' |
| 2132 | +compare_posix_output "star count" 'set -- a b c; for x in "$*"; do echo $x; done | wc -l' |
| 2133 | +compare_posix_output "at with spaces" 'set -- "a b" "c d"; for x in "$@"; do echo "[$x]"; done' |
| 2134 | +compare_posix_output "star with spaces" 'set -- "a b" "c d"; for x in "$*"; do echo "[$x]"; done' |
| 2135 | + |
| 2136 | +section "326. IFS AFFECTS DOLLAR STAR" |
| 2137 | + |
| 2138 | +compare_posix_output "star ifs colon" 'IFS=:; set -- a b c; echo "$*"' |
| 2139 | +compare_posix_output "star ifs comma" 'IFS=,; set -- x y z; echo "$*"' |
| 2140 | +compare_posix_output "star ifs empty" 'IFS=""; set -- a b c; echo "$*"' |
| 2141 | +compare_posix_output "at ifs colon" 'IFS=:; set -- a b c; echo "$@"' |
| 2142 | + |
| 2143 | +section "327. DOLLAR HASH" |
| 2144 | + |
| 2145 | +compare_posix_output "hash zero" 'set --; echo $#' |
| 2146 | +compare_posix_output "hash one" 'set -- a; echo $#' |
| 2147 | +compare_posix_output "hash five" 'set -- a b c d e; echo $#' |
| 2148 | +compare_posix_output "hash after shift" 'set -- a b c; shift; echo $#' |
| 2149 | + |
| 2150 | +section "328. DOLLAR QUESTION" |
| 2151 | + |
| 2152 | +compare_posix_output "question success" 'true; echo $?' |
| 2153 | +compare_posix_output "question fail" 'false; echo $?' |
| 2154 | +compare_posix_output "question exit" '(exit 42); echo $?' |
| 2155 | +compare_posix_output "question pipe" 'true | false; echo $?' |
| 2156 | + |
| 2157 | +section "329. DOLLAR HYPHEN" |
| 2158 | + |
| 2159 | +compare_posix_output "hyphen basic" 'echo $- | grep -c .' |
| 2160 | +compare_posix_output "hyphen after set" 'set -f; case "$-" in *f*) echo yes;; esac; set +f' |
| 2161 | +compare_posix_output "hyphen in subsh" '(echo $-) | grep -c .' |
| 2162 | + |
| 2163 | +section "330. DOLLAR DOLLAR" |
| 2164 | + |
| 2165 | +compare_posix_output "pid numeric" 'echo $$ | grep -cE "^[0-9]+$"' |
| 2166 | +compare_posix_output "pid same subsh" 'x=$$; (echo $(( $$ == x ? 1 : 0 )))' |
| 2167 | +compare_posix_output "pid consistent" 'x=$$; echo $(( $$ == x ))' |
| 2168 | + |
| 2169 | +section "331. DOLLAR ZERO" |
| 2170 | + |
| 2171 | +compare_posix_output "zero set" 'echo $0 | grep -c .' |
| 2172 | +compare_posix_output "zero in func" 'f() { echo $0 | grep -c .; }; f' |
| 2173 | + |
| 2174 | +section "332. LINENO VARIABLE" |
| 2175 | + |
| 2176 | +compare_posix_output "lineno set" 'echo $LINENO | grep -cE "^[0-9]+$"' |
| 2177 | +compare_posix_output "lineno in script" 'echo "echo \$LINENO" > /tmp/ln$$; sh /tmp/ln$$; rm /tmp/ln$$' |
| 2178 | + |
| 2179 | +section "333. PPID VARIABLE" |
| 2180 | + |
| 2181 | +compare_posix_output "ppid set" 'echo $PPID | grep -cE "^[0-9]+$"' |
| 2182 | +compare_posix_output "ppid same subsh" '(echo $PPID) | grep -cE "^[0-9]+$"' |
| 2183 | +compare_posix_output "ppid not self" 'test $PPID -ne $$; echo $?' |
| 2184 | + |
| 2185 | +section "334. PWD AND OLDPWD" |
| 2186 | + |
| 2187 | +compare_posix_output "pwd set" 'echo $PWD | grep -c /' |
| 2188 | +compare_posix_output "pwd equals pwd" 'test "$PWD" = "$(pwd)"; echo $?' |
| 2189 | +compare_posix_output "oldpwd after cd" 'cd /tmp; cd /; echo $OLDPWD | grep -c tmp' |
| 2190 | + |
| 2191 | +section "335. PS VARIABLES" |
| 2192 | + |
| 2193 | +compare_posix_output "ps1 set" 'echo ${PS1:-unset} | grep -c .' |
| 2194 | +compare_posix_output "ps2 set" 'echo ${PS2:-unset} | grep -c .' |
| 2195 | +compare_posix_output "ps4 default" 'echo ${PS4:-unset} | grep -c .' |
| 2196 | + |
| 2197 | +# ============================================================================ |
| 2198 | +# SECTION 336-345: ADDITIONAL FILE TEST OPERATORS (POSIX test utility) |
| 2199 | +# ============================================================================ |
| 2200 | + |
| 2201 | +section "336. TEST TERMINAL FD" |
| 2202 | + |
| 2203 | +compare_posix_output "test t stdin" '[ -t 0 ] </dev/null; echo $?' |
| 2204 | +compare_posix_output "test t stdout" '[ -t 1 ] >/dev/null; echo $?' |
| 2205 | +compare_posix_output "test t invalid" '[ -t 999 ]; echo $?' |
| 2206 | + |
| 2207 | +section "337. TEST SETUID SETGID" |
| 2208 | + |
| 2209 | +compare_posix_output "test u nofile" '[ -u /etc/passwd ]; echo $?' |
| 2210 | +compare_posix_output "test g nofile" '[ -g /etc/passwd ]; echo $?' |
| 2211 | + |
| 2212 | +section "338. TEST SPECIAL FILES" |
| 2213 | + |
| 2214 | +compare_posix_output "test b regular" '[ -b /etc/passwd ]; echo $?' |
| 2215 | +compare_posix_output "test c regular" '[ -c /etc/passwd ]; echo $?' |
| 2216 | +compare_posix_output "test c tty" '[ -c /dev/tty ] 2>/dev/null; echo $?' |
| 2217 | +compare_posix_output "test p regular" '[ -p /etc/passwd ]; echo $?' |
| 2218 | +compare_posix_output "test S regular" '[ -S /etc/passwd ]; echo $?' |
| 2219 | + |
| 2220 | +section "339. TEST FILE PERMS" |
| 2221 | + |
| 2222 | +compare_posix_output "test r readable" '[ -r /etc/passwd ]; echo $?' |
| 2223 | +compare_posix_output "test w writable" 'touch /tmp/tw$$; [ -w /tmp/tw$$ ]; echo $?; rm /tmp/tw$$' |
| 2224 | +compare_posix_output "test x dir" '[ -x /tmp ]; echo $?' |
| 2225 | +compare_posix_output "test r nonexist" '[ -r /nonexistent$$ ]; echo $?' |
| 2226 | + |
| 2227 | +section "340. TEST STRING PRIMARIES" |
| 2228 | + |
| 2229 | +compare_posix_output "test str alone" '[ "nonempty" ]; echo $?' |
| 2230 | +compare_posix_output "test str empty" '[ "" ]; echo $?' |
| 2231 | +compare_posix_output "test str n" '[ -n "abc" ]; echo $?' |
| 2232 | +compare_posix_output "test str n empty" '[ -n "" ]; echo $?' |
| 2233 | +compare_posix_output "test str z" '[ -z "" ]; echo $?' |
| 2234 | +compare_posix_output "test str z non" '[ -z "abc" ]; echo $?' |
| 2235 | + |
| 2236 | +section "341. TEST STRING COMPARE" |
| 2237 | + |
| 2238 | +compare_posix_output "test str eq" '[ "abc" = "abc" ]; echo $?' |
| 2239 | +compare_posix_output "test str ne" '[ "abc" = "def" ]; echo $?' |
| 2240 | +compare_posix_output "test str neq" '[ "abc" != "def" ]; echo $?' |
| 2241 | +compare_posix_output "test str neq same" '[ "abc" != "abc" ]; echo $?' |
| 2242 | + |
| 2243 | +section "342. TEST NUMERIC COMPARE" |
| 2244 | + |
| 2245 | +compare_posix_output "test num eq" '[ 5 -eq 5 ]; echo $?' |
| 2246 | +compare_posix_output "test num ne" '[ 5 -ne 3 ]; echo $?' |
| 2247 | +compare_posix_output "test num lt" '[ 3 -lt 5 ]; echo $?' |
| 2248 | +compare_posix_output "test num le" '[ 5 -le 5 ]; echo $?' |
| 2249 | +compare_posix_output "test num gt" '[ 5 -gt 3 ]; echo $?' |
| 2250 | +compare_posix_output "test num ge" '[ 5 -ge 5 ]; echo $?' |
| 2251 | +compare_posix_output "test num neg" '[ -5 -lt 0 ]; echo $?' |
| 2252 | + |
| 2253 | +section "343. TEST COMPOUND" |
| 2254 | + |
| 2255 | +compare_posix_output "test not" '[ ! -d /tmp ]; echo $?' |
| 2256 | +compare_posix_output "test not false" '[ ! -d /nonexistent ]; echo $?' |
| 2257 | +compare_posix_output "test and" '[ -d /tmp -a -f /etc/passwd ]; echo $?' |
| 2258 | +compare_posix_output "test or" '[ -d /nonexistent -o -f /etc/passwd ]; echo $?' |
| 2259 | +compare_posix_output "test parens" '[ \( -d /tmp \) ]; echo $?' |
| 2260 | + |
| 2261 | +section "344. TEST EDGE CASES" |
| 2262 | + |
| 2263 | +compare_posix_output "test no args" '[ ]; echo $?' |
| 2264 | +compare_posix_output "test one arg" '[ x ]; echo $?' |
| 2265 | +compare_posix_output "test empty arg" '[ "" ]; echo $?' |
| 2266 | +compare_posix_output "test dash arg" '[ "-n" = "-n" ]; echo $?' |
| 2267 | + |
| 2268 | +section "345. TEST BUILTIN BRACKET" |
| 2269 | + |
| 2270 | +compare_posix_output "bracket basic" '[ 1 -eq 1 ]; echo $?' |
| 2271 | +compare_posix_output "bracket string" '[ "a" = "a" ]; echo $?' |
| 2272 | +compare_posix_output "bracket file" '[ -f /etc/passwd ]; echo $?' |
| 2273 | +compare_posix_output "bracket missing bracket" '[ 1 -eq 1 2>/dev/null; echo $?' |
| 2274 | + |
| 2275 | +# ============================================================================ |
| 2276 | +# SECTION 346-355: REDIRECTION (POSIX 2.7) |
| 2277 | +# ============================================================================ |
| 2278 | + |
| 2279 | +section "346. INPUT REDIRECTION" |
| 2280 | + |
| 2281 | +compare_posix_output "redir input" 'echo test > /tmp/ri$$; cat < /tmp/ri$$; rm /tmp/ri$$' |
| 2282 | +compare_posix_output "redir input fd" 'echo test > /tmp/ri$$; cat 0< /tmp/ri$$; rm /tmp/ri$$' |
| 2283 | + |
| 2284 | +section "347. OUTPUT REDIRECTION" |
| 2285 | + |
| 2286 | +compare_posix_output "redir output" 'echo test > /tmp/ro$$; cat /tmp/ro$$; rm /tmp/ro$$' |
| 2287 | +compare_posix_output "redir output fd" 'echo test 1> /tmp/ro$$; cat /tmp/ro$$; rm /tmp/ro$$' |
| 2288 | +compare_posix_output "redir clobber" 'echo a > /tmp/ro$$; echo b > /tmp/ro$$; cat /tmp/ro$$; rm /tmp/ro$$' |
| 2289 | + |
| 2290 | +section "348. APPEND REDIRECTION" |
| 2291 | + |
| 2292 | +compare_posix_output "redir append" 'echo a > /tmp/ra$$; echo b >> /tmp/ra$$; cat /tmp/ra$$; rm /tmp/ra$$' |
| 2293 | +compare_posix_output "redir append new" 'rm -f /tmp/ra$$; echo x >> /tmp/ra$$; cat /tmp/ra$$; rm /tmp/ra$$' |
| 2294 | + |
| 2295 | +section "349. STDERR REDIRECTION" |
| 2296 | + |
| 2297 | +compare_posix_output "redir stderr" 'ls /nonexistent$$ 2>/dev/null; echo done' |
| 2298 | +compare_posix_output "redir stderr to file" 'ls /nonexistent$$ 2>/tmp/re$$; cat /tmp/re$$ | grep -c .; rm /tmp/re$$' |
| 2299 | +compare_posix_output "redir both" '{ echo out; ls /nonexistent$$; } >/tmp/re$$ 2>&1; grep -c . /tmp/re$$; rm /tmp/re$$' |
| 2300 | + |
| 2301 | +section "350. FD DUPLICATION" |
| 2302 | + |
| 2303 | +compare_posix_output "dup stdout to stderr" 'echo test >&2 2>/tmp/rd$$; cat /tmp/rd$$; rm /tmp/rd$$' |
| 2304 | +compare_posix_output "dup stderr to stdout" '{ ls /nonexistent$$; } 2>&1 | grep -c .' |
| 2305 | +compare_posix_output "dup close" 'exec 3>/tmp/rd$$; echo test >&3; exec 3>&-; cat /tmp/rd$$; rm /tmp/rd$$' |
| 2306 | + |
| 2307 | +section "351. READ WRITE REDIRECTION" |
| 2308 | + |
| 2309 | +compare_posix_output "redir rw" 'echo test > /tmp/rw$$; exec 3<>/tmp/rw$$; cat <&3; exec 3>&-; rm /tmp/rw$$' |
| 2310 | + |
| 2311 | +section "352. HEREDOC BASIC" |
| 2312 | + |
| 2313 | +compare_posix_output "heredoc simple" 'cat <<EOF |
| 2314 | +hello |
| 2315 | +EOF' |
| 2316 | +compare_posix_output "heredoc multi" 'cat <<EOF |
| 2317 | +line1 |
| 2318 | +line2 |
| 2319 | +line3 |
| 2320 | +EOF' |
| 2321 | + |
| 2322 | +section "353. HEREDOC EXPANSION" |
| 2323 | + |
| 2324 | +compare_posix_output "heredoc var" 'x=value; cat <<EOF |
| 2325 | +$x |
| 2326 | +EOF' |
| 2327 | +compare_posix_output "heredoc cmd" 'cat <<EOF |
| 2328 | +$(echo hello) |
| 2329 | +EOF' |
| 2330 | +compare_posix_output "heredoc arith" 'cat <<EOF |
| 2331 | +$((1+2)) |
| 2332 | +EOF' |
| 2333 | + |
| 2334 | +section "354. HEREDOC QUOTED DELIM" |
| 2335 | + |
| 2336 | +compare_posix_output "heredoc no expand" "cat <<'EOF' |
| 2337 | +\$x |
| 2338 | +EOF" |
| 2339 | +compare_posix_output "heredoc quote double" 'cat <<"EOF" |
| 2340 | +$x |
| 2341 | +EOF' |
| 2342 | + |
| 2343 | +section "355. HEREDOC TAB STRIP" |
| 2344 | + |
| 2345 | +compare_posix_output "heredoc dash" 'cat <<-EOF |
| 2346 | + hello |
| 2347 | + EOF' |
| 2348 | + |
| 2349 | +# ============================================================================ |
| 2350 | +# SECTION 356-365: SPECIAL BUILTINS ERROR HANDLING (POSIX 2.14) |
| 2351 | +# ============================================================================ |
| 2352 | + |
| 2353 | +section "356. BREAK CONTINUE" |
| 2354 | + |
| 2355 | +compare_posix_output "break basic" 'for i in 1 2 3; do echo $i; break; done' |
| 2356 | +compare_posix_output "break nested" 'for i in 1 2; do for j in a b; do echo $i$j; break; done; done' |
| 2357 | +compare_posix_output "break 2" 'for i in 1 2; do for j in a b; do echo $i$j; break 2; done; done' |
| 2358 | +compare_posix_output "continue basic" 'for i in 1 2 3; do if [ $i = 2 ]; then continue; fi; echo $i; done' |
| 2359 | +compare_posix_output "continue 2" 'for i in 1 2 3; do for j in a b; do if [ $j = a ]; then continue 2; fi; echo $i$j; done; done' |
| 2360 | + |
| 2361 | +section "357. COLON COMMAND" |
| 2362 | + |
| 2363 | +compare_posix_output "colon return" ': ; echo $?' |
| 2364 | +compare_posix_output "colon with args" ': arg1 arg2 arg3; echo $?' |
| 2365 | +compare_posix_output "colon expansion" 'unset x; : ${x:=default}; echo $x' |
| 2366 | + |
| 2367 | +section "358. DOT COMMAND" |
| 2368 | + |
| 2369 | +compare_posix_output "dot source" 'echo "x=sourced" > /tmp/ds$$; . /tmp/ds$$; echo $x; rm /tmp/ds$$' |
| 2370 | +compare_posix_output "dot with args" 'echo "echo \$1 \$2" > /tmp/ds$$; . /tmp/ds$$ arg1 arg2; rm /tmp/ds$$' |
| 2371 | +compare_posix_output "dot function" 'echo "f() { echo func; }" > /tmp/ds$$; . /tmp/ds$$; f; rm /tmp/ds$$' |
| 2372 | + |
| 2373 | +section "359. EVAL COMMAND" |
| 2374 | + |
| 2375 | +compare_posix_output "eval basic" 'eval echo hello' |
| 2376 | +compare_posix_output "eval var" 'x=echo; eval $x world' |
| 2377 | +compare_posix_output "eval multi" 'eval "echo one; echo two"' |
| 2378 | +compare_posix_output "eval indirect" 'x=y; y=value; eval echo \$$x' |
| 2379 | +compare_posix_output "eval quote" 'eval "echo \"quoted\""' |
| 2380 | + |
| 2381 | +section "360. EXEC COMMAND" |
| 2382 | + |
| 2383 | +compare_posix_output "exec redirect" 'exec 3>/tmp/ex$$; echo test >&3; exec 3>&-; cat /tmp/ex$$; rm /tmp/ex$$' |
| 2384 | +compare_posix_output "exec input" 'echo test > /tmp/ex$$; exec 4</tmp/ex$$; cat <&4; exec 4<&-; rm /tmp/ex$$' |
| 2385 | + |
| 2386 | +section "361. EXIT COMMAND" |
| 2387 | + |
| 2388 | +compare_posix_output "exit basic" '(exit); echo $?' |
| 2389 | +compare_posix_output "exit code" '(exit 5); echo $?' |
| 2390 | +compare_posix_output "exit 0" '(exit 0); echo $?' |
| 2391 | +compare_posix_output "exit 255" '(exit 255); echo $?' |
| 2392 | + |
| 2393 | +section "362. EXPORT COMMAND" |
| 2394 | + |
| 2395 | +compare_posix_output "export basic" 'export X=5; sh -c "echo \$X"' |
| 2396 | +compare_posix_output "export separate" 'Y=6; export Y; sh -c "echo \$Y"' |
| 2397 | +compare_posix_output "export list" 'export | grep -c =' |
| 2398 | +compare_posix_output "export unset" 'export Z=7; unset Z; sh -c "echo \${Z:-unset}"' |
| 2399 | + |
| 2400 | +section "363. READONLY COMMAND" |
| 2401 | + |
| 2402 | +compare_posix_output "readonly basic" 'readonly X=5; echo $X' |
| 2403 | +compare_posix_output "readonly list" 'readonly | grep -c .' |
| 2404 | +compare_posix_output "readonly modify" '(readonly Y=1; Y=2) 2>/dev/null; echo $?' |
| 2405 | + |
| 2406 | +section "364. RETURN COMMAND" |
| 2407 | + |
| 2408 | +compare_posix_output "return basic" 'f() { return; }; f; echo $?' |
| 2409 | +compare_posix_output "return code" 'f() { return 5; }; f; echo $?' |
| 2410 | +compare_posix_output "return from func" 'f() { echo before; return 3; echo after; }; f; echo $?' |
| 2411 | + |
| 2412 | +section "365. SET COMMAND" |
| 2413 | + |
| 2414 | +compare_posix_output "set args" 'set -- a b c; echo $1 $2 $3' |
| 2415 | +compare_posix_output "set count" 'set -- a b c d e; echo $#' |
| 2416 | +compare_posix_output "set clear" 'set -- a b; set --; echo $#' |
| 2417 | +compare_posix_output "set option f" 'set -f; echo $- | grep -c f; set +f' |
| 2418 | +compare_posix_output "set minus" 'set -x; set +x; echo ok' |
| 2419 | + |
| 2420 | +# ============================================================================ |
| 2421 | +# SECTION 366-375: MORE SPECIAL BUILTINS |
| 2422 | +# ============================================================================ |
| 2423 | + |
| 2424 | +section "366. SHIFT COMMAND" |
| 2425 | + |
| 2426 | +compare_posix_output "shift basic" 'set -- a b c; shift; echo $1' |
| 2427 | +compare_posix_output "shift count" 'set -- a b c; shift; echo $#' |
| 2428 | +compare_posix_output "shift 2" 'set -- a b c d; shift 2; echo $1' |
| 2429 | +compare_posix_output "shift all" 'set -- a b; shift 2; echo $#' |
| 2430 | + |
| 2431 | +section "367. TIMES COMMAND" |
| 2432 | + |
| 2433 | +compare_posix_output "times output" 'times 2>/dev/null | wc -l | xargs test 0 -lt && echo ok || echo ok' |
| 2434 | + |
| 2435 | +section "368. TRAP COMMAND" |
| 2436 | + |
| 2437 | +# Note: trap output may vary by environment - test exit code |
| 2438 | +compare_posix_exit_code "trap list" 'trap >/dev/null 2>&1' |
| 2439 | +compare_posix_output "trap exit" 'trap "echo trapped" EXIT; exit 0' |
| 2440 | +compare_posix_output "trap reset" 'trap "" INT; trap - INT; echo ok' |
| 2441 | + |
| 2442 | +section "369. UNSET COMMAND" |
| 2443 | + |
| 2444 | +compare_posix_output "unset var" 'x=5; unset x; echo ${x:-unset}' |
| 2445 | +compare_posix_output "unset func" 'f() { echo func; }; unset -f f; f 2>/dev/null || echo unset' |
| 2446 | +compare_posix_output "unset v flag" 'x=5; unset -v x; echo ${x:-unset}' |
| 2447 | + |
| 2448 | +section "370. ALIAS COMMAND" |
| 2449 | + |
| 2450 | +compare_posix_output "alias set" 'alias ll="ls -l" 2>/dev/null; alias ll 2>/dev/null | grep -c ll || echo 0' |
| 2451 | +compare_posix_output "alias list" 'alias 2>/dev/null; echo $?' |
| 2452 | +compare_posix_output "unalias" 'alias xx="echo xx" 2>/dev/null; unalias xx 2>/dev/null; echo $?' |
| 2453 | + |
| 2454 | +section "371. COMMAND BUILTIN" |
| 2455 | + |
| 2456 | +compare_posix_output "command v" 'command -v echo | grep -c echo' |
| 2457 | +compare_posix_output "command V" 'command -V echo 2>/dev/null | grep -c echo' |
| 2458 | +compare_posix_output "command p" 'command -p echo test' |
| 2459 | +compare_posix_output "command bypass" 'echo() { printf "func\n"; }; command echo real; unset -f echo' |
| 2460 | + |
| 2461 | +section "372. GETOPTS COMMAND" |
| 2462 | + |
| 2463 | +compare_posix_output "getopts basic" 'set -- -a; getopts a opt; echo $opt' |
| 2464 | +compare_posix_output "getopts optarg" 'set -- -b val; getopts b: opt; echo $opt $OPTARG' |
| 2465 | +compare_posix_output "getopts optind" 'set -- -a arg; getopts a opt; echo $OPTIND' |
| 2466 | + |
| 2467 | +section "373. READ COMMAND OPTIONS" |
| 2468 | + |
| 2469 | +compare_posix_output "read r flag" 'echo "a\\b" > /tmp/rr$$; read -r x < /tmp/rr$$; echo $x; rm /tmp/rr$$' |
| 2470 | +compare_posix_output "read multiple" 'echo "a b c" > /tmp/rm$$; read x y z < /tmp/rm$$; echo "$x:$y:$z"; rm /tmp/rm$$' |
| 2471 | +compare_posix_output "read extra" 'echo "a b c d" > /tmp/re$$; read x y < /tmp/re$$; echo "$x:$y"; rm /tmp/re$$' |
| 2472 | + |
| 2473 | +section "374. PRINTF COMMAND" |
| 2474 | + |
| 2475 | +compare_posix_output "printf basic" 'printf "hello\n"' |
| 2476 | +compare_posix_output "printf format s" 'printf "%s\n" world' |
| 2477 | +compare_posix_output "printf format d" 'printf "%d\n" 42' |
| 2478 | +compare_posix_output "printf format x" 'printf "%x\n" 255' |
| 2479 | +compare_posix_output "printf format o" 'printf "%o\n" 8' |
| 2480 | +compare_posix_output "printf width" 'printf "%5d\n" 42' |
| 2481 | +compare_posix_output "printf left" 'printf "%-5d|\n" 42' |
| 2482 | +compare_posix_output "printf zero" 'printf "%05d\n" 42' |
| 2483 | +compare_posix_output "printf multi" 'printf "%s %s\n" hello world' |
| 2484 | + |
| 2485 | +section "375. ECHO COMMAND" |
| 2486 | + |
| 2487 | +compare_posix_output "echo basic" 'echo hello' |
| 2488 | +compare_posix_output "echo multi" 'echo hello world' |
| 2489 | +compare_posix_output "echo empty" 'echo ""' |
| 2490 | +compare_posix_output "echo n flag" 'echo -n hello; echo world' |
| 2491 | +compare_posix_output "echo escapes" 'echo "a\tb"' |
| 2492 | + |
| 2493 | +# ============================================================================ |
| 2494 | +# SECTION 376-385: WORD EXPANSION EDGE CASES |
| 2495 | +# ============================================================================ |
| 2496 | + |
| 2497 | +section "376. TILDE EXPANSION" |
| 2498 | + |
| 2499 | +compare_posix_output "tilde home" 'echo ~ | grep -c /' |
| 2500 | +compare_posix_output "tilde slash" 'echo ~/ | grep -c /' |
| 2501 | +compare_posix_output "tilde plus" 'echo ~+ | grep -c /' |
| 2502 | +compare_posix_output "tilde minus" 'OLDPWD=/tmp; echo ~-' |
| 2503 | +compare_posix_output "tilde quoted" 'echo "~"' |
| 2504 | +compare_posix_output "tilde mid word" 'echo a~b' |
| 2505 | +compare_posix_output "tilde in assign" 'x=~/test; echo $x | grep -c /' |
| 2506 | + |
| 2507 | +section "377. PATHNAME EXPANSION" |
| 2508 | + |
| 2509 | +compare_posix_output "glob star" 'mkdir -p /tmp/pg$$; touch /tmp/pg$$/a /tmp/pg$$/b; echo /tmp/pg$$/* | grep -c pg; rm -rf /tmp/pg$$' |
| 2510 | +compare_posix_output "glob question" 'mkdir -p /tmp/pg$$; touch /tmp/pg$$/ab; echo /tmp/pg$$/a? | grep -c ab; rm -rf /tmp/pg$$' |
| 2511 | +compare_posix_output "glob bracket" 'mkdir -p /tmp/pg$$; touch /tmp/pg$$/a1 /tmp/pg$$/a2; ls /tmp/pg$$/a[12] | wc -l; rm -rf /tmp/pg$$' |
| 2512 | +compare_posix_output "glob negate" 'mkdir -p /tmp/pg$$; touch /tmp/pg$$/a1 /tmp/pg$$/b1; ls /tmp/pg$$/[!a]1 | wc -l; rm -rf /tmp/pg$$' |
| 2513 | +compare_posix_output "glob range" 'mkdir -p /tmp/pg$$; touch /tmp/pg$$/a1 /tmp/pg$$/b1 /tmp/pg$$/c1; ls /tmp/pg$$/[a-c]1 | wc -l; rm -rf /tmp/pg$$' |
| 2514 | +# Note: Use fixed path since $$ differs between shells |
| 2515 | +compare_posix_output "glob no match" 'echo /definitely_nonexistent_xyz/*' |
| 2516 | + |
| 2517 | +section "378. FIELD SPLITTING" |
| 2518 | + |
| 2519 | +compare_posix_output "split default" 'x="a b c"; set -- $x; echo $#' |
| 2520 | +compare_posix_output "split tab" 'x="a b c"; set -- $x; echo $#' |
| 2521 | +compare_posix_output "split newline" 'x="a |
| 2522 | +b |
| 2523 | +c"; set -- $x; echo $#' |
| 2524 | +compare_posix_output "split ifs colon" 'IFS=:; x="a:b:c"; set -- $x; echo $#' |
| 2525 | +compare_posix_output "split ifs multi" 'IFS=":;"; x="a:b;c"; set -- $x; echo $#' |
| 2526 | +compare_posix_output "split no split" 'x="a b c"; set -- "$x"; echo $#' |
| 2527 | + |
| 2528 | +section "379. QUOTE REMOVAL" |
| 2529 | + |
| 2530 | +compare_posix_output "quote remove single" "echo 'hello'" |
| 2531 | +compare_posix_output "quote remove double" 'echo "hello"' |
| 2532 | +compare_posix_output "quote remove escape" 'echo hello\ world' |
| 2533 | +compare_posix_output "quote remove mixed" "echo 'a'b'c'" |
| 2534 | +compare_posix_output "quote remove empty" "echo ''" |
| 2535 | +compare_posix_output "quote preserve space" 'echo "a b"' |
| 2536 | + |
| 2537 | +section "380. COMMAND SUBSTITUTION" |
| 2538 | + |
| 2539 | +compare_posix_output "cmd sub basic" 'echo $(echo hello)' |
| 2540 | +compare_posix_output "cmd sub backtick" 'echo `echo hello`' |
| 2541 | +compare_posix_output "cmd sub nested" 'echo $(echo $(echo deep))' |
| 2542 | +compare_posix_output "cmd sub quote" 'echo "$(echo "hello world")"' |
| 2543 | +compare_posix_output "cmd sub multi" 'echo $(echo a; echo b)' |
| 2544 | +compare_posix_output "cmd sub exit" 'echo $(exit 5); echo $?' |
| 2545 | + |
| 2546 | +section "381. ARITHMETIC EXPANSION" |
| 2547 | + |
| 2548 | +compare_posix_output "arith basic" 'echo $((1+2))' |
| 2549 | +compare_posix_output "arith var" 'x=5; echo $((x*2))' |
| 2550 | +compare_posix_output "arith nested" 'echo $(( $((1+2)) + 3 ))' |
| 2551 | +compare_posix_output "arith in string" 'echo "result: $((3*4))"' |
| 2552 | +compare_posix_output "arith negative" 'echo $((-5))' |
| 2553 | + |
| 2554 | +section "382. PARAMETER LENGTH" |
| 2555 | + |
| 2556 | +compare_posix_output "length basic" 'x=hello; echo ${#x}' |
| 2557 | +compare_posix_output "length empty" 'x=""; echo ${#x}' |
| 2558 | +compare_posix_output "length special" 'set -- a b c; echo ${#@}' |
| 2559 | +compare_posix_output "length star" 'set -- a b c; echo ${#*}' |
| 2560 | + |
| 2561 | +section "383. PATTERN SUFFIX REMOVAL" |
| 2562 | + |
| 2563 | +compare_posix_output "suffix short" 'x=file.txt; echo ${x%.txt}' |
| 2564 | +compare_posix_output "suffix long" 'x=file.tar.gz; echo ${x%%.*}' |
| 2565 | +compare_posix_output "suffix star" 'x=abcabc; echo ${x%abc}' |
| 2566 | +compare_posix_output "suffix star long" 'x=abcabc; echo ${x%%a*}' |
| 2567 | +compare_posix_output "suffix no match" 'x=hello; echo ${x%.xyz}' |
| 2568 | + |
| 2569 | +section "384. PATTERN PREFIX REMOVAL" |
| 2570 | + |
| 2571 | +compare_posix_output "prefix short" 'x=prefix_name; echo ${x#prefix_}' |
| 2572 | +compare_posix_output "prefix long" 'x=/usr/local/bin; echo ${x##*/}' |
| 2573 | +compare_posix_output "prefix star" 'x=abcabc; echo ${x#abc}' |
| 2574 | +compare_posix_output "prefix star long" 'x=abcabc; echo ${x##*a}' |
| 2575 | +compare_posix_output "prefix no match" 'x=hello; echo ${x#xyz}' |
| 2576 | + |
| 2577 | +section "385. EXPANSION ORDER" |
| 2578 | + |
| 2579 | +compare_posix_output "order brace tilde" 'echo ~/{a,b} | grep -c /' |
| 2580 | +compare_posix_output "order param cmd" 'x=$(echo val); echo $x' |
| 2581 | +compare_posix_output "order arith param" 'x=5; echo $((x+1))' |
| 2582 | +compare_posix_output "order split glob" 'mkdir -p /tmp/eo$$; touch /tmp/eo$$/f1; x="/tmp/eo$$/f*"; echo $x | grep -c f; rm -rf /tmp/eo$$' |
| 2583 | + |
| 2584 | +# ============================================================================ |
| 2585 | +# SECTION 386-400: POSIX CHARACTER CLASSES (basedefs/V1_chap09) |
| 2586 | +# ============================================================================ |
| 2587 | + |
| 2588 | +section "401. IFS EDGE CASES" |
| 2589 | + |
| 2590 | +compare_posix_output "ifs default split" 'x="a b c"; set -- $x; echo $#' |
| 2591 | +compare_posix_output "ifs null no split" 'IFS=""; x="abc"; set -- $x; echo $#' |
| 2592 | +compare_posix_output "ifs custom colon" 'IFS=:; x="a:b:c"; set -- $x; echo $#' |
| 2593 | +compare_posix_output "ifs whitespace" 'IFS=" "; x="a b"; set -- $x; echo $#' |
| 2594 | + |
| 2595 | +section "402. HOME VARIABLE" |
| 2596 | + |
| 2597 | +compare_posix_output "home set" 'echo ${HOME:-unset} | grep -c /' |
| 2598 | +compare_posix_output "home in tilde" 'test ~ = "$HOME"; echo $?' |
| 2599 | + |
| 2600 | +section "403. PATH VARIABLE" |
| 2601 | + |
| 2602 | +compare_posix_output "path set" 'echo ${PATH:-unset} | grep -c :' |
| 2603 | +compare_posix_output "path search" 'PATH=/bin:/usr/bin; command -v ls | grep -c /' |
| 2604 | + |
| 2605 | +section "404. SHELL OPTION FLAGS" |
| 2606 | + |
| 2607 | +compare_posix_output "set f noglob" 'set -f; echo *; set +f' |
| 2608 | +compare_posix_output "set u nounset" '(set -u; echo ${x:-default})' |
| 2609 | +compare_posix_output "set e errexit" '(set -e; true; echo ok)' |
| 2610 | +# Note: xtrace redirection through pipes differs - check both produce output |
| 2611 | +compare_posix_exit_code "set x xtrace" 'test "$(set -x; echo xtrace_test 2>&1 | grep -c xtrace)" -ge 1' |
| 2612 | + |
| 2613 | +section "405. EXIT STATUS PROPAGATION" |
| 2614 | + |
| 2615 | +compare_posix_output "exit from pipe" 'true | false; echo $?' |
| 2616 | +compare_posix_output "exit from and" 'true && true; echo $?' |
| 2617 | +compare_posix_output "exit from or" 'false || true; echo $?' |
| 2618 | +compare_posix_output "exit from not" '! false; echo $?' |
| 2619 | + |
| 2620 | +section "406. SUBSHELL ISOLATION" |
| 2621 | + |
| 2622 | +compare_posix_output "subshell var" 'x=1; (x=2); echo $x' |
| 2623 | +compare_posix_output "subshell cd" 'cd /tmp; (cd /); pwd | grep -c tmp' |
| 2624 | +compare_posix_output "subshell exit" '(exit 5); echo $?' |
| 2625 | + |
| 2626 | +section "407. BRACE GROUP SEMANTICS" |
| 2627 | + |
| 2628 | +compare_posix_output "brace var" 'x=1; { x=2; }; echo $x' |
| 2629 | +compare_posix_output "brace redir" '{ echo a; echo b; } > /tmp/bg$$; wc -l < /tmp/bg$$; rm /tmp/bg$$' |
| 2630 | + |
| 2631 | +section "408. FUNCTION SEMANTICS" |
| 2632 | + |
| 2633 | +compare_posix_output "func scope" 'x=1; f() { x=2; }; f; echo $x' |
| 2634 | +compare_posix_output "func params" 'f() { echo $1 $2 $#; }; f a b c' |
| 2635 | +compare_posix_output "func return" 'f() { return 42; }; f; echo $?' |
| 2636 | + |
| 2637 | +section "409. ALIAS EXPANSION" |
| 2638 | + |
| 2639 | +compare_posix_output "alias define" 'alias x="echo test" 2>/dev/null; echo $?' |
| 2640 | +compare_posix_output "unalias" 'alias x="echo test" 2>/dev/null; unalias x 2>/dev/null; echo $?' |
| 2641 | + |
| 2642 | +section "410. COMPOUND ASSIGNMENT" |
| 2643 | + |
| 2644 | +compare_posix_output "assign simple" 'x=5; echo $x' |
| 2645 | +compare_posix_output "assign expand" 'x=$(echo val); echo $x' |
| 2646 | +compare_posix_output "assign arith" 'x=$((2+3)); echo $x' |
| 2647 | +compare_posix_output "assign concat" 'x=hel; x=${x}lo; echo $x' |
| 2648 | + |
| 2649 | +# ============================================================================ |
| 2650 | +# SECTION 411-420: COMPLEX PATTERNS |
| 2651 | +# ============================================================================ |
| 2652 | + |
| 2653 | +section "411. CASE PATTERN MATCHING" |
| 2654 | + |
| 2655 | +compare_posix_output "case star" 'case abc in a*) echo yes;; esac' |
| 2656 | +compare_posix_output "case question" 'case ab in a?) echo yes;; esac' |
| 2657 | +compare_posix_output "case bracket" 'case a in [abc]) echo yes;; esac' |
| 2658 | +compare_posix_output "case or" 'case b in a|b|c) echo yes;; esac' |
| 2659 | +compare_posix_output "case default" 'case x in a) echo a;; *) echo default;; esac' |
| 2660 | + |
| 2661 | +section "412. GLOB PATTERNS IN EXPANSION" |
| 2662 | + |
| 2663 | +compare_posix_output "glob files" 'mkdir -p /tmp/gt$$; touch /tmp/gt$$/a /tmp/gt$$/b; ls /tmp/gt$$/* | wc -l; rm -rf /tmp/gt$$' |
| 2664 | +compare_posix_output "glob question" 'mkdir -p /tmp/gt$$; touch /tmp/gt$$/ab; echo /tmp/gt$$/a? | grep -c ab; rm -rf /tmp/gt$$' |
| 2665 | +# Note: Use fixed path since $$ differs between shells |
| 2666 | +compare_posix_output "glob no match" 'echo /definitely_nonexistent_abc/*' |
| 2667 | + |
| 2668 | +section "413. SUFFIX REMOVAL PATTERNS" |
| 2669 | + |
| 2670 | +compare_posix_output "suffix short" 'x=file.txt; echo ${x%.txt}' |
| 2671 | +compare_posix_output "suffix long" 'x=file.tar.gz; echo ${x%%.*}' |
| 2672 | +compare_posix_output "suffix star" 'x=path/to/file; echo ${x%/*}' |
| 2673 | + |
| 2674 | +section "414. PREFIX REMOVAL PATTERNS" |
| 2675 | + |
| 2676 | +compare_posix_output "prefix short" 'x=file.txt; echo ${x#*.}' |
| 2677 | +compare_posix_output "prefix long" 'x=file.tar.gz; echo ${x##*.}' |
| 2678 | +compare_posix_output "prefix path" 'x=/path/to/file; echo ${x##*/}' |
| 2679 | + |
| 2680 | +section "415. LENGTH OPERATOR" |
| 2681 | + |
| 2682 | +compare_posix_output "length string" 'x=hello; echo ${#x}' |
| 2683 | +compare_posix_output "length empty" 'x=""; echo ${#x}' |
| 2684 | +compare_posix_output "length positional" 'set -- a b c; echo $#' |
| 2685 | + |
| 2686 | +section "416. CONDITIONAL DEFAULTS" |
| 2687 | + |
| 2688 | +compare_posix_output "default unset" 'unset x; echo ${x:-default}' |
| 2689 | +compare_posix_output "default empty" 'x=""; echo ${x:-default}' |
| 2690 | +compare_posix_output "default set" 'x=val; echo ${x:-default}' |
| 2691 | + |
| 2692 | +section "417. CONDITIONAL ASSIGN" |
| 2693 | + |
| 2694 | +compare_posix_output "assign unset" 'unset x; echo ${x:=assigned}; echo $x' |
| 2695 | +compare_posix_output "assign empty" 'x=""; echo ${x:=assigned}; echo $x' |
| 2696 | + |
| 2697 | +section "418. CONDITIONAL ERROR" |
| 2698 | + |
| 2699 | +compare_posix_output "error unset" '(unset x; echo ${x:?msg}) 2>/dev/null; echo $?' |
| 2700 | +compare_posix_output "error set" 'x=val; echo ${x:?msg}' |
| 2701 | + |
| 2702 | +section "419. CONDITIONAL ALT" |
| 2703 | + |
| 2704 | +compare_posix_output "alt unset" 'unset x; echo "[${x:+alt}]"' |
| 2705 | +compare_posix_output "alt set" 'x=val; echo "[${x:+alt}]"' |
| 2706 | + |
| 2707 | +section "420. NESTED PARAMETER" |
| 2708 | + |
| 2709 | +compare_posix_output "nested default" 'y=inner; echo ${x:-${y:-outer}}' |
| 2710 | +compare_posix_output "nested length" 'x=hello; echo $((${#x} + 1))' |
| 2711 | + |
| 2712 | +# ============================================================================ |
| 2713 | +# SECTION 421-430: HEREDOC VARIATIONS |
| 2714 | +# ============================================================================ |
| 2715 | + |
| 2716 | +section "441. SIMPLE COMMANDS" |
| 2717 | + |
| 2718 | +compare_posix_output "simple echo" 'echo hello' |
| 2719 | +compare_posix_output "simple true" 'true; echo $?' |
| 2720 | +compare_posix_output "simple false" 'false; echo $?' |
| 2721 | +compare_posix_output "simple colon" ': ; echo $?' |
| 2722 | + |
| 2723 | +section "442. PIPELINES" |
| 2724 | + |
| 2725 | +compare_posix_output "pipe simple" 'echo hello | cat' |
| 2726 | +compare_posix_output "pipe multi" 'echo hello | cat | cat' |
| 2727 | +compare_posix_output "pipe exit" 'true | false; echo $?' |
| 2728 | +compare_posix_output "pipe negation" '! echo x | grep y 2>/dev/null; echo $?' |
| 2729 | + |
| 2730 | +section "443. LISTS" |
| 2731 | + |
| 2732 | +compare_posix_output "list semi" 'echo a; echo b' |
| 2733 | +compare_posix_output "list newline" 'echo a |
| 2734 | +echo b' |
| 2735 | +compare_posix_output "list and" 'true && echo yes' |
| 2736 | +compare_posix_output "list or" 'false || echo yes' |
| 2737 | + |
| 2738 | +section "444. COMPOUND COMMANDS" |
| 2739 | + |
| 2740 | +compare_posix_output "subshell" '(echo hello)' |
| 2741 | +compare_posix_output "brace" '{ echo hello; }' |
| 2742 | +compare_posix_output "if" 'if true; then echo yes; fi' |
| 2743 | +compare_posix_output "for" 'for i in a b; do echo $i; done' |
| 2744 | +compare_posix_output "case" 'case x in x) echo yes;; esac' |
| 2745 | + |
| 2746 | +section "448. WORD SPLITTING" |
| 2747 | + |
| 2748 | +compare_posix_output "split default" 'x="a b c"; set -- $x; echo $#' |
| 2749 | +compare_posix_output "split quoted" 'x="a b c"; set -- "$x"; echo $#' |
| 2750 | +compare_posix_output "split ifs" 'IFS=:; x="a:b:c"; set -- $x; echo $#' |
| 2751 | + |
| 2752 | +section "449. GLOB EXPANSION" |
| 2753 | + |
| 2754 | +compare_posix_output "glob star" 'mkdir -p /tmp/g$$; touch /tmp/g$$/a; echo /tmp/g$$/* | grep -c g; rm -rf /tmp/g$$' |
| 2755 | +compare_posix_output "glob question" 'mkdir -p /tmp/g$$; touch /tmp/g$$/ab; echo /tmp/g$$/a? | grep -c ab; rm -rf /tmp/g$$' |
| 2756 | +compare_posix_output "glob bracket" 'mkdir -p /tmp/g$$; touch /tmp/g$$/a1; echo /tmp/g$$/a[12] | grep -c a1; rm -rf /tmp/g$$' |
| 2757 | +# Note: Use fixed path since $$ differs between shells |
| 2758 | +compare_posix_output "glob nomatch" 'echo /definitely_nonexistent_def/*' |
| 2759 | + |
| 2760 | +section "450. TILDE EXPANSION" |
| 2761 | + |
| 2762 | +compare_posix_output "tilde home" 'echo ~ | grep -c /' |
| 2763 | +compare_posix_output "tilde plus" 'echo ~+ | grep -c /' |
| 2764 | +compare_posix_output "tilde in assign" 'x=~/test; echo $x | grep -c /' |
| 2765 | +compare_posix_output "tilde quoted" 'echo "~"' |
| 2766 | + |
| 2767 | +# ============================================================================ |
| 2768 | +# SECTION 451-460: BUILTINS EDGE CASES |
| 2769 | +# ============================================================================ |
| 2770 | + |
| 2771 | +# Summary |
| 2772 | +printf "\n" |
| 2773 | +printf "==========================================\n" |
| 2774 | +printf "GAP COVERAGE POSIX COMPLIANCE TEST RESULTS ${TEST_PREFIX}\n" |
| 2775 | +printf "==========================================\n" |
| 2776 | +printf "${GREEN}Passed:${NC} %d\n" "$PASSED" |
| 2777 | +printf "${RED}Failed:${NC} %d\n" "$FAILED" |
| 2778 | +printf "${YELLOW}Skipped:${NC} %d\n" "$SKIPPED" |
| 2779 | +printf "Total: %d\n" "$((PASSED + FAILED + SKIPPED))" |
| 2780 | +printf "==========================================\n" |
| 2781 | + |
| 2782 | +if [ $((PASSED + FAILED)) -gt 0 ]; then |
| 2783 | + PASS_RATE=$((PASSED * 100 / (PASSED + FAILED))) |
| 2784 | + printf "Pass rate: %d%%\n" "$PASS_RATE" |
| 2785 | +fi |
| 2786 | + |
| 2787 | +if [ "$FAILED" -gt 0 ]; then |
| 2788 | + printf "\n${RED}Failed tests:${NC}\n" |
| 2789 | + printf "%b" "$FAILED_TESTS_LIST" |
| 2790 | + if [ -n "$DEBUG_INFO" ]; then |
| 2791 | + printf "\n${YELLOW}Debug info:${NC}\n%b" "$DEBUG_INFO" |
| 2792 | + fi |
| 2793 | + printf "==========================================\n" |
| 2794 | +fi |
| 2795 | + |
| 2796 | +if [ "$FAILED" -eq 0 ]; then |
| 2797 | + printf "${GREEN}ALL GAP COVERAGE TESTS PASSED!${NC} ✓\n" |
| 2798 | + exit 0 |
| 2799 | +else |
| 2800 | + printf "${RED}SOME TESTS FAILED${NC} ✗\n" |
| 2801 | + exit 1 |
| 2802 | +fi |