[BUG] The Bash tool in Claude Code treats `2>&1` as literal arguments when used with pipes, instead of as stderr redirection.
Bug Report: Claude Code Bash Tool Incorrectly Parses stderr Redirection with Pipes
Summary
The Bash tool in Claude Code treats 2>&1 as literal arguments when used with pipes, instead of as stderr redirection.
Reproduction
1. Direct command (FAILS)
echo "test" 2>&1 | cat
Expected: test
Actual: test 2
2. With bash -c wrapper (WORKS)
bash -c 'echo "test" 2>&1 | cat'
Output: test ✅
3. Create and run this test script (WORKS)
cat > test-bug.sh << 'EOF'
#!/bin/bash
echo "Test 1: Direct in script"
echo "test" 2>&1 | cat
echo -e "\nTest 2: Command with stderr"
ls /nonexistent 2>&1 | head -1
echo -e "\nTest 3: npm with redirection"
npm --version 2>&1 | head -1
EOF
chmod +x test-bug.sh
./test-bug.sh
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
- Cannot capture build output with errors:
npm run build 2>&1 | tee build.log - Cannot filter combined output:
command 2>&1 | grep error - Standard shell patterns fail unexpectedly
Workaround
Use bash -c:
bash -c 'npm run build 2>&1 | head -20'
Root Cause
The Bash tool appears to parse the command before passing to shell, incorrectly tokenizing 2>&1 as arguments rather than recognizing it as a redirection operator. This only happens when combined with pipes (|).
Environment
- Claude Code CLI
- Platform: Linux/WSL2
- Confirmed: 2025-08-06
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗