Repository navigation
Description
[Bug 批量] 命令替换状态污染 / heredoc / case 模式 / 参数替换 / 路径转换 / 重定向 / 调试钩子 —— 20+ 个与 GNU Bash 的兼容性差异(含最小复现)
背景:如何发现这些现有测试抓不到的问题
rubash 已通过 Rust 单元测试和 GNU Bash 上游测试(scripts/run-bash-upstream-tests.sh),但用真实集成级脚本(跨层交互:命令替换 × 函数链 × heredoc × 外部命令 × 路径)探测时,发现了 20+ 个兼容性差异。核心结论:问题集中在「跨层交互」与「执行后状态污染」,这是单元测试与上游单层测试覆盖不到的区域。
复现环境:winuxsh 0.10.2 / rubash 0.2.2(1)-release,Windows 11,安装版二进制。
所有最小复现脚本见 winuxsh 仓库 scripts/probe/repro/(run-repros.sh 批量运行,输出 repro-results.txt 可作铁证)。
一、根因类:命令替换($(...))执行后的状态污染(症状多、疑似同一根因)
以下症状均依赖前置状态(先执行若干命令替换/管道后触发),单独运行时可能正常——这正是它们躲过现有测试的原因。复现请按序运行完整脚本。
1.1 深层函数链的赋值捕获命令替换输出丢失
level3() { out="$(sed -n 's/^A="\${A:-\([^}]*\)}"$/\1/p' file)"; printf '%s' "$out"; }
level2() { out="$(level3)"; printf '%s' "$out"; }
level1() { out="$(level2)"; printf '%s' "$out"; }
R="$(level1)"; echo "[$R]" # bash: 值; rubash: 空(间歇)- 触发条件:≥3 层函数 + 每层用
out="$(...)"赋值捕获 + 最内层外部命令。printf '%s' "$(level2)"(参数内捕获)则正常。 - 附:
scripts/probe/repro/repro-03-deep-chain.sh(注:需按 probe35 前置状态复现,间歇性)。
1.2 状态污染后 cd 把「变量绝对路径」当相对路径拼接 PWD
# 前置: 任意多层命令替换执行后
cd "$HOME" # bash: PWD=C:/Users/x
echo "$PWD" # rubash: C:/当前路径/C:/Users/x (拼接!)
cd "${HOME}/repo" # 同样拼接, PWD 越来越长- 字面路径(
/c/Users/x、C:\Users\x)正常;仅变量展开的绝对路径出错。无前置命令替换时正常(probe-cd2.sh 对照)。 - 附:
scripts/probe/probe-state-dep.sh(B 段复现)。
1.3 污染后 printf 输出损坏/丢失、函数参数传递丢失
printf 'X=[%s]\n' "$S2" # 整行输出丢失
printf 'direct=[%s]\n' "$(sed ...)" # 格式串 \n 变字面 n
preview_compact a b c d # 第 4 个参数变 unbound ($4)- 附:
scripts/probe/repro/repro-12-debug-hooks.sh运行序列后的 printf 行为(probe38 T6/T7、dbg9 记录)。
1.4 顶层命令替换后,函数内命令替换间歇失败
- 顶层
X="$(func)"执行后,后续函数内的Y="$(cmd)"返回空(probe33/34/35 系列记录)。同一脚本多次运行结果不一致(概率性)。
二、确定性语义差异(每次必现)
2.1 heredoc 变量展开依赖「接收者」
cat > f <<EOF; echo "$HOME" # 展开 ✓
while read l; do :; done <<EOF; echo "$HOME" # 不展开, 输出字面 $HOME ✗- bash 两种上下文都展开。rubash 仅「重定向到命令」时展开,
while read/done <<EOF不展开。 - 附:
repro-01-heredoc-context.sh
2.2 嵌套命令替换 $(f "$(g)") 返回空
inner() { printf 'inner'; }
outer() { printf '%s:%s' "$1" "$(printf '%s' "$1" | tr a-z A-Z)"; }
echo "$(outer "$(inner)")" # bash: inner:INNER; rubash: :- 附:
repro-02-nested-cmdsubst.sh(B 变体引号残留")
2.3 函数内 local x="$2"(带初始化)对含 ~ 的值做 tilde 展开
T='~'; f() { local x="$2"; printf '%s' "$x"; }
f a "${T}/repo" # bash: ~/repo; rubash: C:/Users/x/repo- 先
local x再赋值则安全(probe15 对照)。附:repro-04-local-tilde.sh
2.4 case 模式不支持变量展开与转义
q='"'; v='"quoted"'
case "$v" in "$q"*) echo MATCH;; esac # bash: MATCH; rubash: 不匹配/解析失败
case "$v" in \"*) echo MATCH;; esac # 同样失败, 甚至报 "case: command not found"- 仅字面模式(如
x)、*winuxsh_*))正常。附:repro-05-case-pattern.sh
2.5 参数替换:替换值反斜杠被删、换行丢失
t="a{newline}b"
r="${t//\{newline\}/\n}" # bash: a\nb(两字符); rubash: anb (反斜杠被删)
N="$(printf '\n')"
r2="${t//\{newline\}/$N}" # bash: 两行; rubash: ab (换行丢失)
r3="${t//\{newline\}/\\n}" # 双反斜杠同样被删- 附:
repro-06-subst-values.sh
2.6 参数替换 pattern 的 [}] 字符类被 } 提前终止
o="abc}"
echo "${o%[}]}" # bash: abc; rubash: abc}]} (pattern 解析错乱)2.7 ${P##*\\} 参数展开导致栈溢出崩溃(0xC00000FD)
P="C:\\Users\\x\\repo"
Q="${P##*\\}" # bash: repo; rubash: thread 'main' has overflowed its stack, 进程崩溃- 严重:可被任意脚本触发崩溃(DoS)。附:
repro-07-crash-stackoverflow.sh
三、管道 / 子 shell
3.1 管道无法运行特殊内建
set -o | head -3 # bash: 选项列表; rubash: "pipeline command could not execute: builtin command not found"- 特殊内建(set/export/...)进管道失败;普通内建(printf)正常。附:
repro-08-pipe-builtin.sh
3.2 子 shell 内 set -e 泄漏到父脚本
( set -e; false; echo should-not ) # bash: 子 shell 退出, 父继续
echo "rc=$?" # rubash: 父脚本直接终止, 此行不执行- 附:
repro-09-subshell-errexit.sh
四、路径处理(Windows 特有)
4.1 反斜杠路径被自动转成 /c/... 形式(读取环境变量、赋值时)
echo "${LOCALAPPDATA}" # bash: C:\Users\x\AppData\Local; rubash: /c/Users/x/AppData/Local
P="C:\\Users\\x\\repo" # 赋值后 P=/c/Users/x/repo4.2 外部命令打不开 /c/... 路径(内外不一致)
head -1 "/c/Users/x/file" # rubash: head: cannot open '/c/Users/x/file' (winuxcmd 是 Windows 原生, 不认 /c/)- 重定向(内建)用原生路径、外部命令参数用
/c/路径——同一路径两种形态,脚本无法同时满足两者。 - 附:
repro-10-paths.sh
4.3 参数替换结果再次经过路径转换
Q="${P//\\//}" # 结果与 P 的实际形态不一致, 再被转换五、重定向
5.1 2> file / 2>> file stderr 重定向未生效
echo "err" 2> file # bash: stderr 进文件; rubash: err 直通终端, 文件为空2>&1合并、>stdout、fd 重定向正常;仅2>系列失效。附:repro-13-stderr-redirect.sh
六、调试钩子缺失/异常(影响外部调试工具兼容)
PS4='DBG> '; set -x; X=1; set +x # bash: 前缀 DBG> ; rubash: 固定 + (PS4 不生效)
trap 'echo D' DEBUG; : # DEBUG trap 不触发
trap 'echo R' RETURN; f # RETURN trap 不触发
trap 'echo E' EXIT # EXIT trap 不触发 (trap -p 注册可见但退出不执行)
echo "${BASH_VERSINFO[*]}" # unset (bash 有数组)- 已工作(对照):
set -x/-v/-u/-e、trap ERR、BASH_SOURCE/FUNCNAME/BASH_LINENO/LINENO、set -o/shopt列表、BASH_VERSION。 - 附:
repro-12-debug-hooks.sh、scripts/probe/probe-debug-hooks.sh
七、工作正常的特性(对照,避免误报)
数组/关联数组(含稀疏、unset、+=)、字符串操作全集(#/##/%/%%/替换/大小写)、算术(**、16#、let)、[[ ]](glob、=~、&&、-gt、-d)、函数(local、return、export -f、declare -f)、compgen -b(61 内置)、glob/nullglob、here-string、&>、fd 重定向(>、2>&1、n>&m)、head 多文件带头(GNU 语义)。
八、测试机制建议(为什么现有测试抓不到,如何补)
- 差分测试套件(differential testing):同一脚本分别在 GNU Bash 与 rubash 运行,逐字节对比 stdout/stderr/退出码。脚本库按「跨层交互」设计:heredoc×接收者矩阵、嵌套命令替换×深度、函数链×赋值捕获、路径形式矩阵(正斜杠/反斜杠//c/)×内外命令、重定向矩阵(2>/2>>/&>/2>&1)、调试钩子矩阵、状态污染序列(前置命令替换后复测同一操作)。
- 状态污染回归测试:对 1.1-1.4 的每类症状,固化「前置序列 + 复测」脚本进 CI(
tests/下,跑 winuxsh 二进制而非仅 Rust 单测)——因为问题只在跨语句累积状态下出现。 - 调试钩子补齐:PS4 生效、trap DEBUG/RETURN/EXIT、BASH_VERSINFO——让外部工具(bash -x 调试器、编辑器插件)直接可用,而不是自造调试器。
- 崩溃防护:2.7 的栈溢出应加正则/深度护栏;建议给 rubash 的展开器加 fuzz 测试(cargo-fuzz 或 proptest)覆盖参数替换 pattern。
- 路径策略:建议 rubash 内部统一用 Windows 原生路径(正斜杠),对外部命令不转换或仅转换到
C:/形态;/c/仅作为输入兼容(cd/文件打开时识别),输出一律原生——避免 winuxcmd 等 Windows 原生工具打不开/c/路径。
附:复现脚本清单(winuxsh 仓库 scripts/probe/)
repro/repro-01-heredoc-context.sh~repro-13-stderr-redirect.sh(13 个,含预期注释)repro/run-repros.sh→repro-results.txt(批量运行铁证)probe-debug-hooks.sh/probe-errexit.sh/probe-paths.sh/probe-matrix.sh/probe-confirm.sh/probe-confirm2.sh/probe-cd2.sh/probe-state-dep.sh/probe-deepchain2.sh
注:1.1-1.4 为状态依赖(间歇/需前置),其余为确定性必现。以上问题均可在安装版 winuxsh 0.10.2 + 本仓库 HEAD 复现。
九、复核补充(复查全部 40+ probe 后补录的遗漏项)
9.1 $'...' ANSI-C quoting 完全不支持(确定性)
echo "[$'\t']" # bash: <tab>; rubash: 字面 $'\t'
echo "[$'\n']" # bash: 换行; rubash: 字面
echo "[$'a\x41']" # bash: aA; rubash: 字面- 影响:依赖转义字符串构造 tab/控制字符的脚本全部失效。
9.2 命令替换参数内的字面 ~(双引号内)被 tilde 展开(确定性)
echo "$(printf '%s' "~/repo")" # bash: ~/repo; rubash: C:/Users/x/repo (展开!)
T='~'
echo "$(printf '%s' "${T}/repo")" # bash: ~/repo; rubash: ~/repo (变量值安全, 对照)- 与 2.3(local 初始化)同族:rubash 在命令替换的词法阶段对字面
~做展开,双引号不防护。
9.3 外部|外部管道在命令替换中输出丢失(间歇/状态依赖, 归 1.x 类)
out="$(sed -n '$pat' file | head -n 1)" # bash: 值; rubash: 空(间歇)
out2="$(printf 'x\n' | head -n 1)" # 内建|外部 正常(对照)- probe20/probe31 复现为空;单跑有时成功——同一状态污染根因。
9.4 函数内「含反斜杠变量值」作为外部命令参数被破坏(间歇/状态依赖, 归 1.x 类)
t() { local p; p='s/^A="\${A:-\([^}]*\)}"$/\1/p'; printf '%s' "$(sed -n "$p" f)"; }- probe40 复现(sed 参数含
\${/\(时输出空);单跑有时成功。
十、差分测试复核(用 GNU Bash 逐字节对照后:更正 1 条误报 + 新增 5 条)
差分测试套件见
scripts/probe/difftest/(驱动 + 20 用例,bash vs winuxsh 逐字节对比 stdout/stderr/rc)。它抓到了单侧探测时的误报与漏报。
10.1 更正:§9.1 $'...' ANSI-C quoting 是误报(winuxsh 实际支持)
X=$'\t' # 赋值(词法层面) → 两 shell 均得到真 tab ✓
echo "[$X]" # 一致
echo "[$'\t']" # 双引号内: bash 同样字面 $'\t' (这不是 ANSI-C 场景, 不算差异)- 原 §9.1 的探测写法是
"$'\t'"(双引号内),bash 也不展开,不构成兼容性差异。撤回该条目。差分测试的价值示例。
10.2 新增:case 模式含引号字符的字面模式也不匹配
v='"quoted"' # 值以单引号字符开头
case "$v" in
'"'*) echo MATCH ;; # bash: MATCH; rubash: NO (引号字符模式解析失败)
esac- §2.4 扩展:不只变量/转义模式,任何含引号字符的模式都失败;纯字母模式正常。
10.3 新增:[[ ]] 带引号的 glob 模式不匹配
[[ "a b" == *" "* ]] && echo MATCH # bash: MATCH; rubash: 无输出- 与 case 模式同族:
[[ ]]的 RHS 含引号字符(*" "*)时匹配失败;无引号 glob(a*)与=~正则正常。
10.4 新增:嵌套命令替换的引号残留
echo "$(printf 'v=[%s]' "$(printf 'mid')")" # bash: v=[mid]; rubash: v=[]" (引号残留)
echo "$(printf 'n=%s' "$(printf 'deep')")" # bash: n=deep; rubash: n=deep" (尾部 " 残留)- §2.2 变体:嵌套
$(...)不仅返回空,还会在外层输出尾部残留一个"。
10.5 新增:FUNCNAME 不含 main(顶层调用者)
t2() { echo "${FUNCNAME[*]}"; }; t2
# bash: t2 main; rubash: t2- §6 调试钩子补充:
FUNCNAME数组缺顶层main元素;BASH_SOURCE/LINENO行为待核(LINENO 值有偏移)。
10.6 新增:参数替换 pattern 的 \\ 转义不匹配
P2="C:\\work\\dir\\file.txt"
echo "${P2//\\//}" # bash: C:/work/dir/file.txt; rubash: C:\work\dir\file.txt (未替换)- §2.5 扩展:不只替换值反斜杠被删,pattern 里的
\\(匹配单个反斜杠)也不生效。
Activity
docs: add issue #20-#26 summary and root-cause-family repair plan (do…
arookieofc commentedon Aug 13, 2026
继续修复 heredoc/命令替换根因:批处理 stdin 现在会先收集 command substitution 内的 heredoc,再检查括号平衡,避免 heredoc body 中的括号触发错误 EOF;command-list substitution 也改用真实 parser 处理 heredoc。验证:part_005 40/40;Bash upstream run-heredoc、run-comsub、run-comsub-eof、run-comsub-posix、run-redir、run-vredir、run-procsub 共 7/7。
arookieofc commentedon Aug 13, 2026
修复参数替换 pattern 的反斜杠语义:
${P//\\\\//}现在可将 Windows 路径分隔符替换为/。验证:part_024 13/13,直接 Bash/Rubash 对照输出均为 C:/work/dir/file.txt。arookieofc commentedon Aug 14, 2026
Fixed in commit f0c1636.
Root cause: redirect targets were flattened to one string before execution, so an unquoted parameter expansion containing whitespace was treated as a literal filename. The common command execution boundary now rejects ambiguous unquoted redirect targets with Bash-compatible status 1 and diagnostic, while quoted targets remain valid.
Verification:
arookieofc commentedon Aug 14, 2026
Fixed in commit 3f90adf. The ambiguous-redirect guard now skips parser redirects marked HereString, so <<< $'alpha\nbeta' is treated as input data rather than a filename while ordinary file/fd redirects remain validated.
Verification: mapfile 25 passed, readarray 12 passed, fd redirect tests 14 passed, and bounded executor integration 1501 passed with 22 remaining failures in unrelated source/exec status, associative key, process substitution, conditional, and fd/process-substitution cases.
arookieofc commentedon Aug 14, 2026
Fixed in commit 16a1165. Ordered redirected output no longer resets a builtin's already-computed exit status. This restores missing-file/invalid-option statuses for source and command -V with stderr redirects; echo now records its own successful status explicitly. Focused source and command-description tests pass, while two exec diagnostic-capture cases remain under investigation.
arookieofc commentedon Aug 14, 2026
Fixed in commit d4a2e78. Option diagnostics from exec with no command operand now go through the redirected builtin output path. Invalid -Z and missing -a arguments therefore honor 2> while redirections remain persistent. Verification: exec-focused executor slice 122/122 and fd redirect tests 14/14.
arookieofc commentedon Aug 14, 2026
2026-08-14 follow-up: the residual executor compatibility slice is now green. This round fixed ordinary-vs-special readonly assignment status propagation, conditional
</>parsing, and ordered fd propagation through eval/compound commands. Verification: executor 1523/1523, CLI 155/155, lib 155/155. The broader issue remains open for the upstream suite families not covered by this slice.arookieofc commentedon Aug 17, 2026
2026-08-17 follow-up (commit 62c64eb): fixed the Windows POSIX-directory physical-PWD bridge exposed by the executor suite. cd -P /; pwd -P now reports / instead of the host repository path. Focused regression and full executor_tests pass (1535/1535). Other #20 command-substitution, case-pattern, path, and debug-hook families remain open.
arookieofc commentedon Aug 17, 2026
2026-08-17 follow-up (commits 349d06e, 784ce7d): fixed a shared ordered-output redirect boundary for builtin printf and pwd. Their direct redirect branches bypassed parse-order fd application; for
>&2 2>file, Bash keeps output on the original stderr but still creates an empty target, while Rubash previously skipped target creation. Both builtins now buffer stdout/stderr through the shared ordered fd owner. Evidence: fd_redirects 16/16; executor command_chaining::part_021 12/12; parser_redirection_tests 68/68; command_chaining::part_080 152/152; run-redir 1/1; cargo check passed. The broader issue-suite rows remain open.caomengxuan666 commentedon Aug 28, 2026
关闭说明
原始报告的 20+ 项差异,大部分已修复:
剩余 cmd sub 状态泄漏已在 #43 中跟踪。历史 issue 不再有参考价值,关闭以减少噪音。
fix(glob): export strmatch for loadables and route GLOBIGNORE through…