Bash Command Redirection Fails with `2>&1` is treated as if the `2` was a separate parameter
Resolved 💬 16 comments Opened Jul 29, 2025 by mattgodbolt Closed Aug 12, 2025
💡 Likely answer: A maintainer (bogini, collaborator)
responded on this thread — see the highlighted reply below.
Bug Description
Many bash commands using 2>&1 fail with things like "ERROR: Task not found "2". It seems like the 2>&1 is not being treated as a single token, and so bash sees 2 >&1 or similar. I've also ended up with 2 files in my directory.
Commands like:
● Bash(npm run ts-check 2>&1 | grep -A5 -B5 "editor.ts")
⎿ ERROR: Task not found: "2"
Which is a bit infurating as Claude really wants to redirect stderr, but gets itself in a loop trying different ways to phrase this. Seems like the 2>&1 isn't being interpreted by the "bash" task properly
Environment Info
- Platform: linux
- Terminal: gnome-terminal
- Version: 1.0.62
- Feedback ID: 609743f9-8414-4e42-915b-f7632a91d508
Errors
[{"error":"Error: Command failed: ps aux | grep -E \"code|cursor|windsurf|idea|pycharm|webstorm|phpstorm|rubymine|clion|goland|rider|datagrip|dataspell|aqua|gateway|fleet|android-studio\" | grep -v grep\n at genericNodeError (node:child_process:959:22)\n at checkExecSyncError (node:child_process:458:27)\n at execSync (node:child_process:278:31)\n at V6 (/$bunfs/root/claude:707:5109)\n at kp (/$bunfs/root/claude:1200:2368)\n at <anonymous> (/$bunfs/root/claude:1767:7177)\n at processTicksAndRejections (native:7:39)","timestamp":"2025-07-29T17:04:00.321Z"},{"error":"Error: **Input Error**\n\nIt looks like there was a problem with the input you provided.\n\n`organizationSlug` is required when providing `issueId`\n\nYou may be able to resolve the issue by addressing the concern and trying again.\n at bvG (/$bunfs/root/claude:1252:12058)\n at processTicksAndRejections (native:7:39)","timestamp":"2025-07-29T17:05:15.214Z"},{"error":"Error: Language not supported while highlighting code, falling back to markdown: \n at VV (/$bunfs/root/claude:1358:1192)\n at map (native:1:11)\n at KV (/$bunfs/root/claude:1358:905)\n at M0W (/$bunfs/root/claude:2457:11043)\n at yC (/$bunfs/root/claude:31:19860)\n at Af (/$bunfs/root/claude:33:42826)\n at wf (/$bunfs/root/claude:33:38349)\n at bM (/$bunfs/root/claude:33:38277)\n at sC (/$bunfs/root/claude:33:38131)\n at VK (/$bunfs/root/claude:33:35194)","timestamp":"2025-07-29T17:08:19.716Z"},{"error":"Error: **Error**\n\nThere was an HTTP 402 error with your request to the Sentry API.\n\nNo budget for Seer Autofix.\n\nYou may be able to resolve the issue by addressing the concern and trying again.\n at bvG (/$bunfs/root/claude:1252:12058)\n at processTicksAndRejections (native:7:39)","timestamp":"2025-07-29T17:08:48.856Z"},{"error":"Error: Request was aborted.\n at new rW (unknown:1:28)\n at new FY (/$bunfs/root/claude:1171:12033)\n at new jJ (/$bunfs/root/claude:1171:12821)\n at makeRequest (/$bunfs/root/claude:1188:3840)\n at processTicksAndRejections (native:7:39)","timestamp":"2025-07-29T17:17:12.033Z"},{"error":"Error: Request was aborted.\n at new rW (unknown:1:28)\n at new FY (/$bunfs/root/claude:1171:12033)\n at new jJ (/$bunfs/root/claude:1171:12821)\n at _createMessage (/$bunfs/root/claude:1179:7955)\n at processTicksAndRejections (native:7:39)","timestamp":"2025-07-29T17:18:40.034Z"},{"error":"Error: 400 {\"type\":\"error\",\"error\":{\"type\":\"invalid_request_error\",\"message\":\"too many total text bytes: 15984631 > 9000000\"}}\n at new rW (unknown:1:28)\n at new FY (/$bunfs/root/claude:1171:12033)\n at new tZ0 (unknown:1:28)\n at generate (/$bunfs/root/claude:1171:12490)\n at makeRequest (/$bunfs/root/claude:1188:5330)\n at processTicksAndRejections (native:7:39)","timestamp":"2025-07-29T17:21:11.870Z"},{"error":"Error: Request was aborted.\n at new rW (unknown:1:28)\n at new FY (/$bunfs/root/claude:1171:12033)\n at new jJ (/$bunfs/root/claude:1171:12821)\n at _createMessage (/$bunfs/root/claude:1179:7955)\n at processTicksAndRejections (native:7:39)","timestamp":"2025-07-29T17:21:25.630Z"},{"error":"Error: Could not load the \"sharp\" module using the linux-x64 runtime\nPossible solutions:\n- Ensure optional dependencies can be installed:\n npm install --include=optional sharp\n- Ensure your package manager supports multi-platform installation:\n See https://sharp.pixelplumbing.com/install#cross-platform\n- Add platform-specific dependencies:\n npm install --os=linux --cpu=x64 sharp\n- Consult the installation documentation:\n See https://sharp.pixelplumbing.com/install\n at <anonymous> (/$bunfs/root/claude:311:53570)\n at <anonymous> (/$bunfs/root/claude:12:838)\n at <anonymous> (/$bunfs/root/claude:312:78)\n at <anonymous> (/$bunfs/root/claude:12:838)\n at <anonymous> (/$bunfs/root/claude:312:79845)\n at <anonymous> (/$bunfs/root/claude:12:838)\n at <anonymous> (/$bunfs/root/claude:1150:1732)\n at processTicksAndRejections (native:7:39)","timestamp":"2025-07-29T17:25:21.197Z"}]
16 Comments
I've been seeing this regularly also, eg.
Currently on 1.0.64 (Claude Code)
When I had Claude Code investigate this issue itself, it provided this URL through its search tools. Based on my somewhat unreliable memory, this wasn't a bug that existed from the beginning—it didn't start occurring until after a specific version release, did it?
I exported the conversation using
/exportand extracted the problematic command.From now on, I'll try adding the following line to
~/.claude/CLAUDE.md:My memory and understanding is this is something introduced fairly recently. I'm pretty sure it used to work.
I created a bug report, but will append here instead:
Bug Report: Claude Code Bash Tool Incorrectly Parses stderr Redirection with Pipes
Summary
The Bash tool in Claude Code treats
2>&1as literal arguments when used with pipes, instead of as stderr redirection.Reproduction
1. Direct command (FAILS)
Expected:
testActual:
test 22. With bash -c wrapper (WORKS)
Output:
test✅3. Create and run this test script (WORKS)
Output: Works correctly, showing stderr is properly redirected ✅
The Problem
| Command | Expected | Claude Bash Tool Output |
|---------|----------|------------------------|
|
echo "test" 2>&1 \| cat|test|test 2❌ ||
ls 2>&1 \| head| (file listing) |ls: cannot access '2': No such file or directory❌ ||
npm run build 2>&1 \| head| (build output) | Error: tries to run "vite build 2" ❌ |Impact
npm run build 2>&1 | tee build.logcommand 2>&1 | grep errorWorkaround
Use
bash -c:Root Cause
The Bash tool appears to parse the command before passing to shell, incorrectly tokenizing
2>&1as arguments rather than recognizing it as a redirection operator. This only happens when combined with pipes (|).Environment
Can confirm on v1.0.69 and v1.0.70 on Ubuntu 24.04.3.
I'll also confirm that this is a recent regression.
Only recently started seeing this within the last week or two. WSL1 ubuntu 22.04 with windows terminal
Until this bug is fixed, you can add this script to your project:
and add these instructions to your CLAUDE.md:
Found 3 possible duplicate issues:
This issue will be automatically closed as a duplicate in 3 days.
🤖 Generated with Claude Code
I imagine that despite saying "Bash" its not actually executing in shell mode and is instead splitting the command into an arg vector.
Even when we explicitly mark this command as prohibited in CLAUDE.md and provide workaround instructions, Claude Code inevitably executes the command by habit, resulting in reactive troubleshooting after encountering errors. Using hooks appears to be the most effective solution. Claude Code implemented hooks for Claude Code.
Going to chime in that I've been seeing this for a while on Windows native via git bash. I assumed it was a git bash bug or something but found this issue instead. It'll try to do
2 >& 1 | HEADor similar on cargo outputs then Cargo goes o a test named 2, we don't have one I guess that's fine, and succeeds instead of failing! But with 0 run tests.@bogini
Since you closed this Issue, I assumed the problem was resolved, so I ran
claude updateand tested with the latest version v1.0.73, but the issue didn't appear to have improved.Claude Code said:
Regression: Shell operator parsing breaks common development patterns
Since v1.0.61, Claude Code incorrectly processes
2>&1and glob patterns in piped commands, breaking workflows that worked in v1.0.60.Impact
2>&1 | ...produces incorrect output (adds literal "2")*.mdbecomes literal "glob")bash -cReproduction
Root Cause
v1.0.61 changed from regex splitting to shell-quote parsing but the reconstruction function only uses
opfield, discarding pattern data and mishandling adjacent operators.Fix Options
D.push(shellQuote.stringify([G]))Test Case
Version History
This is a regression that worked correctly for months before v1.0.61. The fix is straightforward - either revert the problematic change or correct the token reconstruction.
Oh no, a section was described as having a bug that went unfixed for 6 weeks - that's a hallucination! Checking npm shows it was actually 17 days. My apologies.
@Rokurolize This is fixed in
1.0.74Confirmed it seems fixed for me (1.0.77 here):
This issue has been automatically locked since it was closed and has not had any activity for 7 days. If you're experiencing a similar issue, please file a new issue and reference this one if it's relevant.