Skip to content

[BUG] Environment Variables Cleared When Using Pipe Operator in Bash Tool #8318

@andre-c-andersen

Description

@andre-c-andersen

Preflight Checklist

  • I have searched existing issues and this hasn't been reported yet
  • This is a single bug report (please file separate reports for different bugs)
  • I am using the latest version of Claude Code

What's Wrong?

When using the Bash tool to access a custom environment variable, say CLAUDE_AGENT_ID, with a pipe operator, the variable expands to only a newline character instead of its actual value "alpha". The environment variable exists and is visible with env | grep CLAUDE_AGENT_ID, but when piped to another command like wc -c, it loses its value.

What Should Happen?

The CLAUDE_AGENT_ID environment variable should consistently expand to its actual value "alpha" when used in any bash command, including when piped to other commands.

Error Messages/Logs

# Variable exists in environment
$ env | grep CLAUDE_AGENT_ID
CLAUDE_AGENT_ID=alpha

# But expands to only newline when piped
$ echo "$CLAUDE_AGENT_ID" | hexdump -C
00000000  0a                                                |.|
00000001

# Character count shows 1 instead of expected 6
$ echo $CLAUDE_AGENT_ID | wc -c
1

Steps to Reproduce

Please provide clear, numbered steps that anyone can follow to reproduce the issue. Important: Include any necessary code, file contents, or context needed to reproduce the bug. If the issue involves specific files or code, please create a minimal example.

  1. In Claude Code, use the Bash tool to echo the CLAUDE_AGENT_ID:

    echo $CLAUDE_AGENT_ID

    Result: Shows "alpha" (appears correct)

  2. Use the Bash tool to count characters with a pipe:

    echo $CLAUDE_AGENT_ID | wc -c

    Result: Returns 1 (incorrect - should be 6)

  3. Verify the variable exists:

    env | grep CLAUDE_AGENT_ID

    Result: Shows CLAUDE_AGENT_ID=alpha (correct)

  4. Check actual bytes being output:

    echo "$CLAUDE_AGENT_ID" | hexdump -C

    Result: Shows only 0a (newline character)

  5. Run in a new bash subprocess:

    bash -c 'echo "$CLAUDE_AGENT_ID" | wc -c'

    Result: Returns 6 (correct - works in subprocess)

Claude Model

Opus

Is this a regression?

I don't know

Last Working Version

No response

Claude Code Version

1.0.128 (Claude Code)

Platform

Anthropic API

Operating System

Ubuntu/Debian Linux

Terminal/Shell

WSL (Windows Subsystem for Linux)

Additional Information

Anything else that might help us understand the issue?

  • The bug only occurs when using pipes with the environment variable
  • Direct echo without pipes appears to work (shows "alpha")
  • The variable works correctly when executed in a new bash subprocess using bash -c
  • This appears to be a shell state issue where the variable exists in the environment but isn't properly accessible in the current shell context
  • Use case where discovered: I observed this issue when trying to inject basic authentication credentials for curl commands using environment variables (e.g., curl -u "$USERNAME:$PASSWORD" https://api.example.com). The authentication would fail because the variables weren't expanding correctly when piped or used in command substitution.
  • Date observed: 2025-09-28

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:toolsbugSomething isn't workinghas reproHas detailed reproduction stepsplatform:linuxIssue specifically occurs on Linux

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions