Environment Variables and Shell Functions Not Persisting

Resolved 💬 3 comments Opened Mar 5, 2025 by aaronlippold Closed Mar 6, 2025

Bug Description
Bug Report: Environment Variables and Shell Functions Not Persisting in
Claude Code

Issue Description

When sourcing scripts that set environment variables or define shell
functions in Claude Code's Bash tool, these changes don't seem to persist
between command invocations. This makes it difficult to create reliable
toolchains and development environments.

Reproduction Steps

  1. Create a script env.sh that:
  • Adds a directory to PATH
  • Exports environment variables
  • Defines shell functions
  1. Source this script with source env.sh in a Bash command
  2. In a subsequent Bash command, try to:
  • Use a command from the added PATH
  • Access the exported environment variables
  • Call the defined shell functions

Example Code

We created a script like this:

#!/bin/bash
  # env.sh
  ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
  TOOLS_DIR="${ROOT_DIR}/.tools/bin"

  # Add to PATH
  export PATH="${TOOLS_DIR}:$PATH"
  echo "PATH now includes ${TOOLS_DIR}"

  # Export tools directory
  export CERTSUITE_TOOLS_DIR="${TOOLS_DIR}"

  # Define shell function
  task() {
      "${TOOLS_DIR}/task" "$@"
  }
  export -f task

When we run:
``source scripts/env.sh && echo $CERTSUITE_TOOLS_DIR && task --list``

We get:

PATH now includes /path/to/.tools/bin
  /path/to/.tools/bin
  zsh: command not found: task

Despite task() being defined in the sourced script, it's not available in
the same command.

Expected Behavior

  • Environment variables set by sourced scripts should be available
  • Shell functions defined in sourced scripts should be callable
  • PATH additions should be effective for finding tools

Actual Behavior

  • Some environment variables may be printed but don't persist
  • Shell functions don't seem to be recognized even within the same command
  • Commands in added PATH directories aren't found

Impact

This issue makes it very difficult to create reliable development
environments and toolchains in projects using Claude Code. Users must
resort to absolute paths for tools or implement workarounds, which reduces
productivity and makes scripts less maintainable.

Possible Causes

This might be happening because:

  1. Each Bash tool invocation runs in a separate environment
  2. Shell functions aren't properly exported or recognized
  3. Command calls are potentially split into separate processes
  4. There might be restrictions on how environment changes persist

Additional Context

We've tried multiple workarounds including:

  • Using absolute paths instead of PATH additions
  • Exporting dedicated environment variables for tool locations
  • Creating shell functions with export -f
  • Using different mechanisms for function exports

None of these approaches have worked consistently within the Claude Code
interface.

Environment Info

  • Platform: macos
  • Terminal: iTerm.app
  • Version: 0.2.30
  • Feedback ID: b88239ec-c4ae-434b-b3d3-6c79e8c032c4

View original on GitHub ↗

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