Loop variables become empty when piped to another command
Bug Description
When using shell loop variables inside a pipe, the variable value becomes empty. This happens only when there's a pipe inside the loop body.
Reproduction Steps
- Run a simple loop without a pipe - works correctly:
for i in a b c; do echo "val=$i"; done
Output: val=a, val=b, val=c ✓
- Run the same loop with a pipe - variable becomes empty:
for i in a b c; do echo "val=$i" | cat; done
Output: val=, val=, val= ✗
Expected Behavior
The loop variable $i should retain its value when used in a pipeline.
Actual Behavior
The loop variable $i becomes empty when the command is piped to another command.
Impact
This breaks common shell patterns like:
for i in 1 2 3; do echo "mark $i yellow" | nc -N 127.0.0.1 9876; done
Which results in mark yellow being sent (missing the number).
Workaround
Using explicit bash invocation works:
/bin/bash -c 'for i in a b c; do echo "val=$i" | cat; done'
Environment
- Shell: zsh
- Platform: Linux (Arch)
- The issue appears to be in Claude Code's command preprocessing before passing to the shell.
Analysis
The error message shows (eval):1: which suggests Claude Code is using eval to execute commands. The variable substitution appears to happen incorrectly during this evaluation phase when pipes are involved.
This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗