Claude Code 在 Windows 下 Bash 与 PowerShell 冲突问题排查

Claude Code 在 Windows 下 Bash 与 PowerShell 冲突问题排查

一、问题背景

最近在 Windows 环境下使用 Anthropic Claude Code CLI 时,发现 Claude 在执行文件搜索、目录读取、格式化输出等操作时频繁报错。

典型现象如下:

● Bash(Get-ChildItem ...)
⎿ Error:
/usr/bin/bash: Get-ChildItem: command not found

或者:

/usr/bin/bash: Format-Table: command not found

这类报错看起来像是 PowerShell 命令不可用,但真正的问题并不是 PowerShell 本身,而是 Claude 内部执行命令时选错了 Shell Runtime。

在界面表现上,Claude Code 通常会先显示类似 ● Bash(Get-ChildItem ...) 的执行记录,然后紧跟一段 Bash 报错。也就是说,日志前缀已经说明 Claude 使用的是 Bash,而括号里的命令却是 PowerShell Cmdlet。这两个信息放在一起,就是定位问题的关键。


二、问题原因分析

问题的本质是:

Claude 内部 Runtime 使用 Bash 执行了 PowerShell 命令。

例如下面这些命令:

Get-ChildItem
Format-Table
Select-Object

它们都属于 PowerShell Cmdlet。

但是 Claude 内部实际调用的是:

/usr/bin/bash

Bash 无法识别 PowerShell Cmdlet,于是最终出现:

command not found

所以这个问题不是命令写错了,也不是 PowerShell 没安装,而是 Claude 的命令执行路由把 PowerShell 命令交给了 Bash。


三、为什么会出现这个问题

很多 AI Agent 或 CLI 工具默认更偏向 Linux 环境,执行命令时会优先尝试 Bash。

Windows 用户的系统里又经常安装了这些工具:

  • Git Bash
  • MSYS2
  • MinGW
  • Cygwin
  • WSL 相关组件

这些工具通常会在 PATH 中提供 bash.exe

当 Claude Code 自动检测 Shell Provider 时,就可能优先选择 Bash Runtime。于是 Windows 用户明明在终端里使用 PowerShell,Claude 内部却仍然通过 Bash 执行命令。


四、检查 bash 来源

可以使用下面命令检查 Bash 来源:

where bash

如果输出类似:

C:\Program Files\Git\usr\bin\bash.exe

说明系统 PATH 中存在 Git Bash,Claude 有可能自动选择它作为运行时。实际排查时,常见情况是 where bash 输出 Git 安装目录下的 bash.exe,而 Claude 的执行日志又显示 Bash(...),这两个现象基本可以确认 Bash 被选作了内部 Runtime。


五、环境排查过程

首先检查当前 PowerShell 版本:

$PSVersionTable

如果看到系统默认仍然是:

Major 5

说明当前使用的是 Windows 自带的旧版 Windows PowerShell。

需要注意:

  • Windows PowerShell 通常指 PowerShell 5。
  • PowerShell 通常指 PowerShell 7,也就是 pwsh

PowerShell 5 并不是不能用,但对于现代 CLI、Agent、跨平台工具链来说,PowerShell 7 更稳定,也更适合作为默认 Shell。


六、升级 PowerShell 7

可以使用 winget 安装新版 PowerShell:

winget install Microsoft.PowerShell

安装完成后,执行:

pwsh

进入 PowerShell 7。

再次验证版本:

$PSVersionTable

如果看到:

Major 7

说明 PowerShell 7 已经安装成功。

$PSVersionTable 中显示 Major 7 时,说明当前会话已经进入 PowerShell 7。这个结果只能证明外部终端环境已经升级成功,并不代表 Claude 内部 Tool Runner 一定会使用 PowerShell 7。


七、配置 Windows Terminal 默认启动 PowerShell 7

打开 Windows Terminal,进入:

设置 -> 启动 -> 默认配置文件

将默认配置文件从:

Windows PowerShell

修改为:

PowerShell

这里容易混淆:

名称 含义
Windows PowerShell 系统自带 PowerShell 5
PowerShell 新版 PowerShell 7

配置完成后,新打开的 Windows Terminal 标签页应该默认进入 PowerShell,而不是 Windows PowerShell。如果命令提示符中能直接执行 pwsh,并且 $PSVersionTable 显示 7.x,说明终端侧配置已经基本完成。


八、真正的隐藏问题

即使升级到 PowerShell 7,并且 Windows Terminal 默认配置也改成了 PowerShell,问题仍然可能存在。

因为真正的问题不是外部终端,而是:

Claude 内部 Tool Runner 仍然默认使用 Bash。

日志中依旧可以看到:

● Bash(...)

这说明:

  • 外部终端已经是 PowerShell 7。
  • 但 Claude 内部执行命令时仍然在调用 Bash。

所以这个问题属于 Runtime Shell Routing 问题,而不是普通的终端配置问题。


九、最终解决思路

解决方向是让 Claude Code 强制使用 PowerShell Runtime,避免 Bash 执行 PowerShell Cmdlet。

可以在启动 Claude 前设置环境变量:

$env:SHELL="pwsh"
claude

也可以进一步让 Claude 自己检查当前 Runtime、环境变量和配置文件。


十、给 Claude 的 Debug Prompt

可以使用下面这个 Prompt 让 Claude 自检 Shell Runtime 问题:

You are currently executing PowerShell commands inside a Bash runtime.

Evidence:

* Logs show: `● Bash(Get-ChildItem ...)`
* Errors:
  `/usr/bin/bash: Get-ChildItem: command not found`
  `/usr/bin/bash: Format-Table: command not found`

This means the runtime shell is incorrectly set to bash while using PowerShell cmdlets.

Environment:

* Windows 11
* PowerShell 7 installed (`pwsh`)
* Windows Terminal
* Git Bash also exists on PATH

Your task:

1. Diagnose why the runtime shell defaults to bash
2. Detect all shell providers currently available
3. Check environment variables related to shell selection
4. Search Claude config files for shell/runtime settings
5. Switch runtime execution from bash to pwsh
6. Ensure future tool calls use PowerShell instead of bash
7. Validate by successfully executing:
   `Get-ChildItem`
   `Format-Table`
8. Explain every modification before applying it

Important:

* Do NOT continue using bash wrappers for PowerShell commands
* Prefer native PowerShell execution
* Treat this as a runtime configuration debugging task

Before executing any command, first determine whether it is:

* PowerShell syntax
* CMD syntax
* Bash syntax

Then route it to the correct shell runtime.

这个 Prompt 的重点不是让 Claude 继续套 Bash,而是要求它识别 Runtime 错配,并切换到原生 PowerShell。

修复后,观察重点不再是外部终端标题,而是 Claude 的工具执行日志。如果日志中不再出现 ● Bash(Get-ChildItem ...),而是改为 PowerShell 相关执行方式,或者 Claude 能成功执行 Get-ChildItemSelect-ObjectFormat-Table 等 Cmdlet,就说明 Runtime 路由已经恢复正常。


十一、问题本质总结

本次问题本质属于:

AI Agent Shell Routing Failure

也就是:

Bash Runtime
错误执行
PowerShell 命令

这类问题在很多 AI Agent 或自动化工具中都比较常见,例如:

  • Claude Code
  • OpenHands
  • Devin 类 Agent
  • AutoGen
  • MCP Tool Runtime
  • Windows / Linux 混合环境下的自动化工具

当 Agent 自动选择 Shell 时,如果没有正确区分 Windows PowerShell、PowerShell 7、Git Bash、WSL Bash,就很容易出现命令路由错误。


十二、最终推荐环境

组件 推荐
终端 Windows Terminal
Shell PowerShell 7
Shell 命令 优先使用原生 PowerShell
编辑器 Visual Studio Code
AI CLI Claude Code
Linux 环境 WSL,可选

十三、后续优化方向

后续可以继续研究这些方向:

  • Claude Runtime Shell Router
  • MCP Tool Runtime
  • 多 Shell Agent 调度
  • Windows / Linux Hybrid Runtime
  • Agent Skill 自动化生成
  • AI 渗透工作流编排
  • Windows 下 AI Agent 工具链标准化

这类问题表面上是命令报错,本质上是 Agent Runtime 选择错误。排查时不要只盯着单条命令,而要看日志里的执行前缀,例如 Bash(...)PowerShell(...)cmd(...),它们往往能直接暴露真正的问题点。

posted @ 2026-05-11 19:20  0xMouise  阅读(1180)  评论(0)    收藏  举报