Loop variables become empty when piped to another command

Resolved 💬 3 comments Opened Dec 10, 2025 by pmatos Closed Dec 10, 2025

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

  1. 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

  1. 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.

View original on GitHub ↗

This issue has 3 comments on GitHub. Read the full discussion on GitHub ↗