Skip to content

[FEATURE] Expose CLAUDE_SESSION_ID to Bash commands and Claude's context #18629

Description

@thielm

Problem

Claude Code knows the session_id and passes it to hooks/statusline via JSON input. However:

  • Bash commands cannot access it - no environment variable available
  • Claude (the AI) cannot access it - not in Claude's context

When a user asks Claude "What session are we in?" - Claude genuinely cannot answer. The information exists in Claude Code but isn't exposed to Claude or Bash.

Use Cases

  • Multi-session coordination: Multiple Claude sessions working in the same repo need unique identifiers for temp files, locks, or state tracking. Without session_id, workarounds using timing or shared queues are unreliable with concurrent sessions.
  • Session-scoped resources: Creating files or caches tied to a specific session (e.g., git --index-file=/tmp/index-$CLAUDE_SESSION_ID)
  • State management: Tracking which session is doing what in multi-agent workflows
  • Debugging: Understanding session context when investigating issues

Current State

Context Has session_id?
Statusline (JSON input)
Hooks (JSON input)
Bash commands
Claude AI context

The session_id already exists and is passed to external scripts - it just needs to be exposed more broadly.

Proposed Solution

Expose session_id as an environment variable available to Bash commands:

export CLAUDE_SESSION_ID=7617cc48-8480-4d24-b52d-4b107c2a7701

This would allow:

# Session-scoped temp files
TMPFILE="/tmp/claude-work-$CLAUDE_SESSION_ID.txt"

# Session-aware state management  
jq --arg sid "$CLAUDE_SESSION_ID" '.sessions[$sid].status = "working"' state.json

Related Issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    duplicateThis issue or pull request already exists

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions