Skip to content

[bug] P0 脚本内执行脚本: 子脚本 $0 错误(fork 形态继承父 $0 / exec 形态 .\反斜杠路径),${0##*/} 自定位全家崩 #166

Closed

Description

@caomengxuan666

现象

在 rubash 脚本内执行另一个脚本文件(作为命令)时,子脚本看到的 $0 错误:

  • 相对/绝对路径调用(fork 形态): 子 $0 = 父脚本的 $0(原样继承),非所调用的脚本路径;
  • exec 形态: 子 $0 = .\child-noshebang.sh(反斜杠 + 字面 .\ 前缀),GNU 为解析后的绝对路径。

GNU Bash 5.3.0: 子 $0 = 调用时的命令词(./child.sh),exec 时为绝对路径。

生产杀伤: 生态脚本普遍用 ${0##*/} 取自身名(makepkg 的报错前缀/usage、portage、无数构建脚本),rubash 下取到父脚本名或反斜杠全路径;${0##*/} 对反斜杠路径不剥离 → 整个路径泄漏进 printf "$fmt" 时 \r \t 被当转义解释(makepkg 现场出现 D:epo ubash arget-ecosys6\... 乱码前缀)。

最小复现

parent-exec.sh(LF 落盘, 双侧脚本文件运行; child-with-shebang.sh 首行 #!/bin/bash, child-noshebang.sh 无 shebang):

#!/bin/bash
printf 'parent: zero=[%s]\n' "$0"
./child-with-shebang.sh          # 相对 + shebang
./child-noshebang.sh             # 相对 + 无 shebang
"$PWD/child-with-shebang.sh"     # 绝对
exec ./child-noshebang.sh        # exec 形态

子脚本内容: printf 'child: zero=[%s] base=[%s]\n' "$0" "${0##*/}"

形态 GNU 5.3.0 子$0 rubash 8f89767 子$0
相对+shebang ./child-with-shebang.sh parent-exec.sh
相对+无shebang ./child-noshebang.sh parent-exec.sh
绝对+shebang $PWD/child-with-shebang.sh parent-exec.sh
exec 相对 绝对路径 /mnt/.../child-noshebang.sh .\child-noshebang.sh

另: 顶层 rubash.exe D:/fwd/slash/probe.sh 直接调用时 $0 保持正斜杠形式、${0##*/} 正常——bug 只出现在"脚本内执行脚本"的子进程路径上。

makepkg 现场

makepkg.sh --version 在 rubash 下报错前缀为 D:epo ubash arget-ecosys6\makepkg-render\makepkg.sh(${0##*/} 未剥离反斜杠路径 + printf 格式串转义解释),GNU 侧前缀为 makepkg.sh。产物: target-ecosys6/out/pe.{rub,gnu}.out, makepkg.diff。

定位建议

fork-执行子脚本走了进程内 eval 类路径未重置 $0;exec 路径做了 Windows 反斜杠规范化(.\ 前缀)。对照 GNU: execute_cmd.c shell_execve() ENOEXEC 分支以命令词为 $0 重新进入 shell;shebang 由内核/execute_disk_command 处理,$0=命令词。

版本: rubash 8f89767 (target-wire/debug), 对照 WSL GNU Bash 5.3.0(1)-release。

Activity

  1. caomengxuan666 commented on Sep 26, 2026

    @caomengxuan666
    MemberAuthor

    wt/m3-argv0 分支已修(commit 1e4d5762 fork 形态 + cec9d55f exec 形态):

    • fork 形态(子 $0 继承父 $0):script_name_value 读链被父级 BASH_ARGV0/顶层名遮蔽;execute_direct_shell_script 现按 shell.c:1613 在子脚本入口重绑 BASH_ARGV0=脚本词(export 过的 BASH_ARGV0 例外,对应 variables.c:1547 set_argv0)。
    • exec 形态(.\child.sh 反斜杠):exec 内建现按 builtins/exec.def 的 full_pathname(general.c:909-922:非绝对词前置 cwd、去 . 分量)把脚本目标解析为正斜杠绝对路径再包装子 shell。

    统一矩阵(含 exec 无 shebang 形态 matrix2)双侧逐字节一致;exec 后 $0=绝对路径与 GNU 一致。FUNCNAME 轴顺带修复了顶层(含 source 中)不可见语义(variables.c:1812 make_funcname_visible / evalfile.c:257 source 帧不改变可见性);残余 declare -p FUNCNAME 函数内缺值已另开 issue。

  2. caomengxuan666 commented on Sep 26, 2026

    @caomengxuan666
    MemberAuthor

    已在 e9a8256 修复并关闭(cec9d55f,exec 形态 + 1e4d5762 fork 形态):exec 形态 $0 = full_pathname(builtins/exec.def + general.c:909-922);fork 形态 $0 重绑同 #171;BASH_ARGV0 镜像与 export 导入优先按 variables.c:1520-1532/1547-1555;unset BASH_ARGV0 后 $0 保持最后赋值,脚本边界清除(__RUBASH_ARGV0_AFTER_UNSET 槽)。matrix2(exec 无 shebang)双侧 diff=0。

  3. added
    bug
    P0-critical
    area:path
    on Oct 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P0-criticalarea:pathbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions