claude --bg sessions inherit the daemon's environment (first terminal), not the calling shell's
Summary
claude --bg gives the new session the environment of the running claude daemon, not the environment of the shell that ran claude --bg. The daemon keeps the environment of whichever terminal started it first, for as long as it lives (more than ten hours here). So every later background session, started from any terminal, carries that first terminal's variables.
It matters when those variables identify the terminal. Terminal apps export per-pane identity variables. Orca, for example, sets ORCA_PANE_KEY, ORCA_TAB_ID and ORCA_AGENT_HOOK_*. A background session started from pane B then carries pane A's identity, and anything that keys on those variables attributes it to pane A.
Environment
Claude Code 2.1.287, macOS 26 (Darwin 25.5.0).
Evidence
Variable names only, read with ps eww -p <pid>:
- The
claude daemon run --origin transientprocess holds 15ORCA_*names, among themORCA_PANE_KEY,ORCA_TAB_ID,ORCA_WORKTREE_IDandORCA_AGENT_HOOK_ENDPOINT. - A
--bg-spareprocess the daemon started 32 seconds before the read, before anyclaude --bgcall could shape it, carries the sameORCA_PANE_KEY,ORCA_TAB_IDandORCA_TERMINAL_HANDLEas the daemon. - A session started with
claude --bgfrom a different shell has anORCA_PANE_KEYequal to the daemon's.
Repro
- In terminal A, export
FOO_IDENTITY=a, then start the daemon, for example with a firstclaude --bg "hi". - In terminal B, export
FOO_IDENTITY=b,BAR_MARKER=1, and an emptyBAZ=, then runclaude --bg "print env". - In the new session,
FOO_IDENTITYisa,BAR_MARKERis absent, and an empty value set in B does not override A's.
Nothing in the calling shell's environment reaches the session. Unsetting, blanking or overriding a variable before claude --bg has no effect.
Expected
A background session gets the environment of the shell that ran claude --bg. At the least, it does not get another terminal's variables: the daemon could drop variables the caller did not have, or let the caller's values override its own.
Workaround
Restart the daemon from a shell without such variables. That holds until a terminal with them starts the daemon again.
This issue has 1 comment on GitHub. Read the full discussion on GitHub ↗