惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

D
Docker
Simon Willison's Weblog
Simon Willison's Weblog
H
Help Net Security
F
Fortinet All Blogs
H
Heimdal Security Blog
S
Schneier on Security
L
LangChain Blog
博客园 - Franky
酷 壳 – CoolShell
酷 壳 – CoolShell
NISL@THU
NISL@THU
P
Palo Alto Networks Blog
J
Java Code Geeks
博客园 - 【当耐特】
The Last Watchdog
The Last Watchdog
W
WeLiveSecurity
www.infosecurity-magazine.com
www.infosecurity-magazine.com
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
Vulnerabilities – Threatpost
I
InfoQ
Recorded Future
Recorded Future
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
CERT Recently Published Vulnerability Notes
T
Tenable Blog
腾讯CDC
C
Check Point Blog
量子位
M
MIT News - Artificial intelligence
GbyAI
GbyAI
罗磊的独立博客
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
B
Blog
小众软件
小众软件
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
C
CXSECURITY Database RSS Feed - CXSecurity.com
Stack Overflow Blog
Stack Overflow Blog
P
Proofpoint News Feed
P
Privacy & Cybersecurity Law Blog
V2EX - 技术
V2EX - 技术
T
Threatpost
Engineering at Meta
Engineering at Meta
Attack and Defense Labs
Attack and Defense Labs
T
Tailwind CSS Blog
S
Securelist
The Cloudflare Blog
博客园 - 叶小钗
L
LINUX DO - 最新话题
T
Troy Hunt's Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
爱范儿
爱范儿

暗无天日

读:AI Agent 安全日志——从可见性与隐私的两难说起 - 暗无天日 读:AI Agent 生产化——一份从原型到上线的速查清单 - 暗无天日 AI写作的语言指纹——如何让文字不那么像机器 - 暗无天日 读:50 条 Claude Code 技巧——一个工程经理的六个月使用心得 读:AI 辅助开发为什么让 E2E 测试更有价值 - 暗无天日 读:在Emacs中使用Claude Code(Spacemacs适配版) - 暗无天日 Claude Code 背后的工程哲学——读 Agent Harness Engineering 读:Agent Harness Engineering——AI 智能体不只是模型,还有套件 - 暗无天日 browser-harness:让 AI 直接接管你的浏览器 - 暗无天日 读:Security-First CI/CD —— DevSecOps 自动化实践指南 TIL: 数字小键盘的小数点陷阱与行内算术求值 - 暗无天日 读:Immutability 不是万能药,它是一种权衡 - 暗无天日 Conducty:给 Claude Code 加上项目记忆和并行执行能力 - 暗无天日 读 — GitHub Trending 里的 Claude Code 技能包 读 — Prompt Caching 省钱指南 TIL: Emacs 中那些跟鼠标配合的冷门快捷键 - 暗无天日 读:Anvil——把 Emacs 变成 AI 的工具服务器 读:Emacs 代码折叠终极指南 - 暗无天日 读:Clojure 搭车客指南 - 暗无天日 git推送失败后恢复仓库损坏的完整记录 - 暗无天日 多智能体系统的两个有效模式——以及对 Claude Code 用户的启示 - 暗无天日 用 Org Babel 写 Literate 博文:扩展执行 + 定制导出 proced:Emacs 内置的进程查看器 - 暗无天日 从 proced 定制中学到的 Elisp 模式 读:让 Emacs proced 在 macOS 上显示 CPU 和内存 异步编程的函数着色税 - 暗无天日 链式调用的代价:JavaScript 和 Clojure 的共同教训 - 暗无天日 hyperfine:命令行基准测试工具 - 暗无天日 开源包装器的信任陷阱:四个危险信号 - 暗无天日 程序员愿意为 AI 写文档,却不愿为同事写 - 暗无天日 mktemp: Shell 脚本中临时文件的安全陷阱与最佳实践 - 暗无天日 WSL9x —— 在 Windows 9x 里跑 Linux 内核 6.19 用 ox.el 做你想做的事 —— org-export 高级编程指南 读:Hot-wiring the Lisp Machine —— 用纯 Elisp 构建零依赖的 Org 静态站点生成器 Elisp 性能优化的六个实战教训 - 暗无天日 fcitx5 下 Emacs 无法切换输入法的排查 - 暗无天日 ERT 测试交互命令的三种方式 - 暗无天日 SEM Assistant: 当 Elisp 守护进程遇上 LLM 用 dmsg 给 Elisp 加上结构化调试日志 用 org-habit 追踪非每日习惯 - 暗无天日 Clojure X-Men:当编程语言特性变成超能力 - 暗无天日 TIL: 用 diff-hl 在 fringe 中显示 git 变更 读:llm-test —— 用 LLM agent 驱动 Emacs 测试 TIL: AI 时代的橡皮鸭调试 - 暗无天日 fcitx 启动后键盘输入卡顿的排查 - 暗无天日 TIL: 早期网页的图片热区导航 - 暗无天日 读 Seeing the Whole System 用 Emacs 自动生成每周链接推荐 - 暗无天日 读:ASCII control characters in my terminal 读 What to learn - 暗无天日 Lisp 的括号之痛——一个愚人节玩笑揭开的老伤疤 - 暗无天日 一本书该"线性读"还是"并行读" - 暗无天日 读 How to Monetize a Blog:一篇伪装成变现指南的讽刺文 Python Mock 第三方依赖的四种策略 - 暗无天日 Emacs Lisp 热重载实用指南 - 暗无天日 Prot 的 Emacs 配置哲学 - 暗无天日 TIL: 从直播对谈中学到的三个 Emacs 技巧 - 暗无天日 TIL: 自动使用项目虚拟环境的 Python - 暗无天日 TIL: 让 Help buffer 自动获得焦点 一条命令让本地开发用上 HTTPS —— slim 工具介绍 用 fsck 检查和修复 Linux 文件系统 排查Linux进程"卡死"实战:从strace到gdb全流程 - 暗无天日 PostgreSQL 索引:从基础到你可能不知道的高级用法 - 暗无天日 用 .pdbrc 自定义 Python 调试器 ANSI 转义码的标准化现状 - 暗无天日 终端程序的潜规则 - 暗无天日 PARA Org-mode 测试配置 - 暗无天日 AI越强越辣鸡?控制论说这是必然的 - 暗无天日 AI 越强越需要你盯着——反馈循环实操指南 - 暗无天日 你的AI代理正在偷你的密钥——四种你没想到的泄露通道 - 暗无天日 LLM 在 DevOps 中的三种角色 - 暗无天日 写作风格的反建议 - 暗无天日 反驳本质复杂性——Dan Luu 论为什么《没有银弹》错了 - 暗无天日 文件充满了危险——Dan Luu 谈文件系统的可靠性陷阱 - 暗无天日 AI 时代的 PARA 方法:用 Org-mode 和 AI 打造个人知识管理系统 Linux 数据去重学习笔记 - 暗无天日 创建跨平台 ZIP 文件的隐藏陷阱:Extra Field - 暗无天日 X11 Forwarding 排障指南 - 暗无天日 IP欺骗端口扫描:当别人冒充你去扫描别人 - 暗无天日 Linux 输入栈全景解析:从硬件按键到屏幕响应 - 暗无天日 Unix 系统中那些被埋没的配置开关——以 FontConfig 为例 - 暗无天日 在Linux上限制儿童使用电脑 - 暗无天日 GIF不仅仅是一种图片格式——用GIF流做些奇怪的事 - 暗无天日 Leiningen 学习笔记:Clojure 项目构建与管理从入门到实战配置 - 暗无天日 Google SRE Book 读书笔记 - 暗无天日 yes 管道 head 发生了什么 - 暗无天日 为什么 nohup 在 crontab 中不起作用 Bash中的Indirection与Nameref - 暗无天日 Linux PAM 简介 - 暗无天日 从Linux ISO文件启动计算机 - 暗无天日 用 Bash 打造一个Screen Locker 用GitHub Actions自动构建EGO博客 - 暗无天日 blocking I/O 的作用 - 暗无天日 mobileog 手机端同步提示Error:2 No such file 的解决方法 回收 WSL2 VHDX 文件占用空间 使用 org-mode columnview 生成任务列表 - 暗无天日 Emacs 作为 MPD 客户端 - 暗无天日 移动文件路径却不破坏org file link的方法 - 暗无天日 如何合理的导出help link 成HTML - 暗无天日 笑话理解之Biology - 暗无天日
管道中的变量去哪了?——子 shell 作用域陷阱 - 暗无天日
2026-04-25 · via 暗无天日

Ventoy 的 GitHub 上有一个 issue:`porteus-hook.sh` 中 `vtFindFlag` 在管道循环里被设为 1 ,出了循环又变回 0 ,导致回退逻辑永远执行。

这是一个经典的 shell 编程陷阱:管道中的每个命令都运行在子 shell 里,子 shell 对变量的修改不会传回父 shell 。本文以这个 bug 为引子,解释子 shell 作用域的原理,并用实际执行验证不同 shell 的行为差异和多种解决方案。

问题复现

用最简单的例子就能看到问题:

flag=0
echo -e "line1\nline2\nline3" | while read line; do
    flag=1
done
echo "flag=$flag"
flag=0

`flag` 明明在循环里被设成了 1 ,出来还是 0 。变量"丢"了。

为什么会这样

POSIX 规范规定:管道中的每个命令都运行在子 shell( subshell )中。子 shell 是当前 shell 的副本——它继承了所有变量,但对变量的修改不会传回父进程。

来看管道的执行模型:

所以 while read 在管道的右端运行在一个子 shell 里, flag=1 发生在这个子 shell 中。管道结束后,子 shell 退出,修改随之消失。

不同 shell 的行为

这不是"所有 shell 都一样"的事。不同 shell 对管道中最后一段命令的处理不同:

flag=0
echo -e "a\nb\nc" | while read x; do flag=1; done
echo "flag=$flag"

bash

bash -c 'flag=0; echo -e "a\nb\nc" | while read x; do flag=1; done; echo "flag=$flag"'
flag=0

bash 默认遵循 POSIX:管道中所有命令都在子 shell 中运行。

zsh

zsh -c 'flag=0; echo -e "a\nb\nc" | while read x; do flag=1; done; echo "flag=$flag"'
flag=1

zsh 的管道最后一段命令默认在当前 shell 中执行,所以变量修改生效。这是 zsh 的非 POSIX 扩展。

dash

dash -c 'flag=0; printf "%s\n" a b c | while read x; do flag=1; done; echo "flag=$flag"'
flag=0

dash 严格遵循 POSIX ,最后一段也在子 shell 中。注意 dash 的 echo 不支持 -e 参数,所以这里用 printf 生成多行输入。

busybox ash

Ventoy 的 hook 脚本开头是 #!/ventoy/busybox/sh ,用的是 busybox ash 。它的行为和 dash 一样——管道中的 while 在子 shell 中运行:

busybox sh -c 'flag=0; printf "%s\n" a b c | while read x; do flag=1; done; echo "flag=$flag"'
flag=0

(此输出基于 busybox ash 与 dash 同属 POSIX shell 的行为一致性,在 dash 上已验证。)

所以 Ventoy 的 bug 在 busybox ash 下一定会触发。

解决方案

有四种常见方案,各有适用场景。

方案一:进程替换( bash/zsh )

用进程替换 < <(...) 代替管道,让 while 循环在当前 shell 中运行:

bash -c '
flag=0
while read x; do
    flag=1
done < <(echo -e "a\nb\nc")
echo "flag=$flag"
'
flag=1

进程替换的原理: <(...) 在后台运行命令,把输出写入一个临时文件描述符,然后 while read 通过重定向读取这个文件描述符——整个过程没有子 shell 参与。

缺点:不是 POSIX 兼容的语法, dash 和 busybox ash 不支持。

方案二:临时文件

把管道输出存到临时文件, while read 从文件读取:

dash -c '
flag=0
tmpfile=$(mktemp)
printf "%s\n" a b c > "$tmpfile"
while read x; do
    flag=1
done < "$tmpfile"
rm -f "$tmpfile"
echo "flag=$flag"
'
flag=1

因为 while read < file 是输入重定向,不是管道,所以循环在当前 shell 中运行。 POSIX 兼容,所有 shell 都能用。

这就是 Ventoy 实际采用的方案——看 porteus-hook.sh 的源码:

$GREP '`value from`' /usr/* -r | $AWK -F: '{print $1}' > $VTOY_PATH/.porteus
while read vtline; do
    $SED "s#\`value from\`#$vtPath#g" -i $vtline
    vtFindFlag=1
done < $VTOY_PATH/.porteus
rm -f $VTOY_PATH/.porteus

grep | awk 的输出写入临时文件 $VTOY_PATH/.porteuswhile read 通过 < $VTOY_PATH/.porteus 重定向读取。 vtFindFlag 在当前 shell 中被修改,回退逻辑的判断 if [ $vtFindFlag -eq 0 ] 能拿到正确值。

方案三:here-doc

如果数据量小,可以用 here-doc 代替管道:

dash -c '
flag=0
while read x; do
    flag=1
done <<EOF
a
b
c
EOF
echo "flag=$flag"
'
flag=1

here-doc 是内联输入重定向,不创建子 shell 。 POSIX 兼容。

缺点是数据必须硬编码或预先展开,不能来自动态命令输出。

方案四:lastpipe( bash 专用)

bash 4.2+ 提供了 lastpipe 选项,让管道最后一段命令在当前 shell 中执行:

bash -c '
shopt -s lastpipe
flag=0
echo -e "a\nb\nc" | while read x; do
    flag=1
done
echo "flag=$flag"
'
flag=1

注意: lastpipe 要求 job control 关闭(非交互式 shell 中默认关闭),在交互式终端中需要额外处理。

不只是管道

管道是最常见的触发场景,但还有其他操作也会创建子 shell :

flag=0
result=$(flag=1; echo done)
echo "flag=$flag"  
flag=0
{ flag=1; } &
wait
echo "flag=$flag"  
flag=0
(flag=1)
echo "flag=$flag"  
flag=0
flag=0
flag=0

一个简单的判断规则: 如果一段代码可能被 fork() 执行,变量修改就不会传回来。 管道、命令替换、后台执行、括号分组——都涉及 fork()

如何快速排查

如果你的脚本出现了"变量莫名其妙变回原值"的问题,检查变量是否在以下结构中被修改:

结构 是否子 shell 变量能传回?
=cmd1 \ cmd2= 是(两端都是) 不能
while read; do ... done < <(cmd) cmd 是, while 不是 能(仅 bash/zsh )
while read; do ... done < file
var=$(cmd) cmd 不能
cmd & 不能
(cmd) 不能

小结

管道子 shell 的变量作用域问题是 shell 编程中最常遇到的陷阱之一。核心规则: 管道中的命令运行在子 shell 里,变量修改不会传回父 shell 。

解决方案按 POSIX 兼容性排序:

  1. 临时文件 (最通用): cmd > tmp; while read < tmp 。 POSIX 兼容,所有 shell 可用
  2. here-doc :数据量小时最简洁。 POSIX 兼容
  3. 进程替换< <(cmd) 。仅 bash/zsh 支持
  4. lastpipeshopt -s lastpipe 。仅 bash 4.2+

Ventoy 的修复用了方案二(临时文件),这对 busybox ash 环境来说是唯一可靠的选择。