claude-code - 💡(How to fix) Fix [BUG] $TMPDIR resolves to different paths between sandboxed and non-sandboxed Bash invocations [1 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
anthropics/claude-code#48541Fetched 2026-04-16 06:57:18
View on GitHub
Comments
1
Participants
2
Timeline
5
Reactions
0
Timeline (top)
labeled ×4commented ×1

Root Cause

  1. Start a Claude Code session on macOS with sandbox enabled
  2. Run echo $TMPDIR via a sandboxed Bash command — returns /tmp/claude-501
  3. Run echo $TMPDIR via a non-sandboxed Bash command (e.g. one matching excludedCommands) — returns /var/folders/.../T/
  4. Write a file in one context: echo hello > $TMPDIR/test.txt
  5. Try to read it from the other context — file not found because $TMPDIR resolves to a different directory
RAW_BUFFERClick to expand / collapse

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?

$TMPDIR resolves to different directories depending on whether a Bash command runs sandboxed or not:

  • Sandboxed Bash: $TMPDIR = /tmp/claude-501
  • Non-sandboxed Bash: $TMPDIR = /var/folders/sg/.../T/ (macOS default)

Sandbox mode is applied per-command based on the excludedCommands configuration. This means within the same session, one command may run sandboxed (seeing /tmp/claude-501) while another runs unsandboxed (seeing /var/folders/.../T/). Files written to $TMPDIR by a sandboxed command are invisible to an unsandboxed command, and vice versa.

This is compounded by the file tools (Read/Write/Edit), which don't evaluate $TMPDIR at all — the model decides which path to use based on what it previously observed. If the model saw /tmp/claude-501 from an earlier sandboxed Bash invocation, it will pass that literal path to Read/Write even when Bash has since switched to the host $TMPDIR, or vice versa.

The system prompt instructs the model to "always use the $TMPDIR environment variable" for temporary files, but this guidance breaks down when the variable points to different locations across invocations.

What Should Happen?

$TMPDIR should resolve to the same directory across all Bash invocations in a session, regardless of sandbox state. File tools should also have a reliable way to reference the same temp directory.

Steps to Reproduce

  1. Start a Claude Code session on macOS with sandbox enabled
  2. Run echo $TMPDIR via a sandboxed Bash command — returns /tmp/claude-501
  3. Run echo $TMPDIR via a non-sandboxed Bash command (e.g. one matching excludedCommands) — returns /var/folders/.../T/
  4. Write a file in one context: echo hello > $TMPDIR/test.txt
  5. Try to read it from the other context — file not found because $TMPDIR resolves to a different directory

Claude Model

Opus

Is this a regression?

I don't know

Last Working Version

No response

Claude Code Version

2.1.109

Platform

Anthropic API

Operating System

macOS

Terminal/Shell

iTerm2

Additional Information

No response

extent analysis

TL;DR

To fix the issue, ensure that $TMPDIR resolves to the same directory across all Bash invocations in a session, regardless of sandbox state, possibly by setting a fixed value for $TMPDIR at the beginning of the session.

Guidance

  • Investigate setting a fixed value for $TMPDIR at the start of the Claude Code session to ensure consistency across sandboxed and non-sandboxed Bash commands.
  • Modify the excludedCommands configuration to minimize the switching between sandboxed and non-sandboxed modes, if possible, to reduce the impact of the issue.
  • Consider updating the file tools (Read/Write/Edit) to dynamically evaluate $TMPDIR instead of relying on previously observed values.
  • Verify that any solution does not introduce security vulnerabilities, especially when dealing with sandboxing.

Example

No specific code example can be provided without modifying the existing Claude Code or Bash scripts, but setting $TMPDIR to a fixed path at the beginning of a session could look like: export TMPDIR=/tmp/claude-code-session.

Notes

The solution may require adjustments based on the specific security requirements and constraints of the Claude Code environment, especially considering the sandboxing functionality.

Recommendation

Apply a workaround by setting a fixed value for $TMPDIR at the start of the session, as this seems to be the most direct way to ensure consistency across different Bash invocations without waiting for a potential fix in a future version of Claude Code.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

claude-code - 💡(How to fix) Fix [BUG] $TMPDIR resolves to different paths between sandboxed and non-sandboxed Bash invocations [1 comments, 2 participants]