[BUG] $TMPDIR resolves to different paths between sandboxed and non-sandboxed Bash invocations
Preflight Checklist
- [x] I have searched existing issues and this hasn't been reported yet
- [x] This is a single bug report (please file separate reports for different bugs)
- [x] 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
- Start a Claude Code session on macOS with sandbox enabled
- Run
echo $TMPDIRvia a sandboxed Bash command — returns/tmp/claude-501 - Run
echo $TMPDIRvia a non-sandboxed Bash command (e.g. one matchingexcludedCommands) — returns/var/folders/.../T/ - Write a file in one context:
echo hello > $TMPDIR/test.txt - Try to read it from the other context — file not found because
$TMPDIRresolves 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_
This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗