[BUG] Environment variables expand to empty string when command contains pipe
[BUG] Environment variables expand to empty string when command contains pipe
What's Wrong?
Environment variables like $HOME expand to an empty string when the Bash tool command contains a pipe (|). This occurs even when the variable is used before the pipe in the command.
What Should Happen?
Environment variables should expand correctly regardless of whether the command contains pipes. echo "$HOME" | cat should output the same value as echo "$HOME".
Reproduction Steps
- Run a simple command without a pipe:
``bash`
echo "HOME=$HOME"
HOME=/Users/username` ✓
**Result:**
- Run the same variable expansion with a pipe:
``bash``
echo "$HOME" | cat
Result: (empty output) ✗
- Use the variable in a
catargument with a pipe:
``bash`
cat "$HOME/.bashrc" 2>&1 | head -1
cat: /.bashrc: No such file or directory
**Result:** (note the missing /Users/username` prefix)
- Same command without a pipe works:
``bash``
head -1 "$HOME/.bashrc"
Result: (file contents displayed correctly) ✓
Error Messages/Logs
No error messages - the variable silently expands to an empty string.
Is this a regression?
Unknown - I have not tested previous versions.
Environment
- Claude Code version: 2.1.12
- API: Anthropic (direct)
- Model: claude-opus-4-5-20250101
- OS: macOS 26.1 (Darwin 25.1.0)
- Terminal: Terminal.app with zsh
Additional Context
This bug was discovered while building a skill that reads JSON files from ~/Library/Application Support/. The workaround is to have Python handle path expansion using os.path.expanduser('~') instead of relying on shell variable expansion.
This may be related to issue #2859 (pipe characters in quoted args being misinterpreted), as both suggest the Bash tool does unusual preprocessing when pipes are present.
Workaround
Instead of:
cat "$HOME/path/to/file.json" | python3 -c "..."
Use:
python3 -c "
import os
path = os.path.expanduser('~/path/to/file.json')
with open(path) as f:
# process file
"This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗