Skip to content

Commit fea109d

Browse files
authored
Merge pull request #7457 from layer5io/blog/claude/non-interactive-shell
Blog: Why claude code can't find your tools
2 parents 3dc97b6 + 9e4c916 commit fea109d

4 files changed

Lines changed: 285 additions & 7 deletions

File tree

package-lock.json

Lines changed: 11 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@
128128
"@eslint/eslintrc": "^3.3.3",
129129
"@eslint/js": "^9.39.2",
130130
"babel-plugin-module-resolver": "^5.0.0",
131-
"baseline-browser-mapping": "^2.8.32",
132131
"cpx": "^1.5.0",
133132
"cross-env": "^10.0.0",
134133
"env-cmd": "^10.1.0",
9.07 MB
Loading
Lines changed: 274 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,274 @@
1+
---
2+
title: "Why Claude Code Can't Find Your Tools"
3+
subtitle: "Understanding non-interactive shells and the .zshenv fix"
4+
date: 2026-03-09 10:00:00 -0530
5+
author: Layer5 Team
6+
thumbnail: ./claude-code-shell-path.png
7+
darkthumbnail: ./claude-code-shell-path.png
8+
description: "Claude Code runs in a non-interactive shell that never sources .zshrc. Learn why tools vanish and how moving PATH exports to .zshenv fixes it for good."
9+
type: Blog
10+
category: Engineering
11+
tags:
12+
- Engineering
13+
- ai
14+
- Open Source
15+
featured: false
16+
published: true
17+
resource: true
18+
---
19+
20+
21+
import { BlogWrapper } from "../../Blog.style.js";
22+
import { Link } from "gatsby";
23+
import Blockquote from "../../../../reusecore/Blockquote";
24+
import KanvasCTA from "../../../../sections/Kanvas/kanvas-cta";
25+
26+
<BlogWrapper>
27+
28+
<div className="intro">
29+
<p>
30+
You type <code>gh pr list</code> in your terminal and it works perfectly. You ask Claude Code to do the same, and it replies: <em>"command not found: gh"</em>. Your Go toolchain, nvm, pyenv, Homebrew binaries — all invisible to the agent. This is not a bug in Claude Code. It is a fundamental property of how Unix shells start up, and once you understand it, the fix is a one-time, five-minute change.
31+
</p>
32+
</div>
33+
34+
## The Surprise
35+
36+
AI coding assistants like Claude Code, Cline, Aider, and similar tools spawn child shell processes to run commands on your behalf. From where you sit, the terminal looks identical to the one you use every day. But the shell those tools launch is fundamentally different from the one you interact with — and that difference determines which startup files are read, which means it determines what is on your PATH.
37+
38+
The result is a confusing experience: tools you have used for years are suddenly invisible to the agent trying to help you. The culprit is not your installation. It is the shell startup file hierarchy.
39+
40+
## Zsh Startup Files and When They Are Sourced
41+
42+
Zsh loads different configuration files depending on how it was launched. There are four main files, and they are read in this order:
43+
44+
| File | Login shell | Interactive shell | Non-interactive shell |
45+
|---|---|---|---|
46+
| `~/.zshenv` | Yes | Yes | Yes |
47+
| `~/.zprofile` | Yes | No | No |
48+
| `~/.zshrc` | No | Yes | No |
49+
| `~/.zlogin` | Yes | No | No |
50+
51+
The key column is the last one. A *non-interactive, non-login shell* — the kind that Claude Code spawns — sources **only** `~/.zshenv`. Everything else is skipped entirely.
52+
53+
- **`~/.zshenv`** is sourced for every zsh invocation, no matter what. It is the right place for environment variables that must be available universally: `PATH`, `GOPATH`, `JAVA_HOME`, and similar exports.
54+
- **`~/.zprofile`** is sourced for login shells (e.g., when you open a new terminal window or SSH into a machine). Homebrew places its environment setup here on Apple Silicon Macs (`/opt/homebrew/bin/brew shellenv`), which is why Homebrew tools can also go missing.
55+
- **`~/.zshrc`** is sourced only for interactive shells — sessions where you type commands. This is where most developers put everything: aliases, prompt configuration, `nvm`, `pyenv`, `rbenv`,
56+
completions, and — critically — PATH customizations.
57+
- **`~/.zlogin`** is sourced after `~/.zshrc` for login shells. It is rarely used by developers directly.
58+
59+
<Blockquote
60+
quote="A non-interactive, non-login shell sources only ~/.zshenv. Everything in ~/.zshrc is invisible to it — including every PATH export most developers have ever written."
61+
/>
62+
63+
## What PATH a Non-Interactive Shell Actually Sees
64+
65+
You can observe this yourself. Run these two commands and compare the output:
66+
67+
```bash
68+
# What a non-interactive shell sees
69+
zsh -c 'echo $PATH'
70+
71+
# What your interactive shell sees
72+
echo $PATH
73+
```
74+
75+
On a typical macOS developer machine, the non-interactive shell might produce something like:
76+
77+
```
78+
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
79+
```
80+
81+
While your interactive shell shows something far richer:
82+
83+
```
84+
/opt/homebrew/bin:/opt/homebrew/sbin:/Users/you/.nvm/versions/node/v20.11.0/bin:
85+
/Users/you/go/bin:/Users/you/.local/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
86+
```
87+
88+
All those extra paths — Homebrew, nvm, Go binaries — live in `~/.zshrc`, `~/.zprofile`, or in
89+
scripts those files source. A non-interactive shell never reads any of them.
90+
91+
## Diagnosing the Problem
92+
93+
Before changing anything, confirm the symptom. Use `zsh -c` to simulate what Claude Code sees:
94+
95+
```bash
96+
# Does Claude Code's shell see the tool?
97+
zsh -c 'which gh'
98+
zsh -c 'which go'
99+
zsh -c 'which node'
100+
101+
# Does your interactive shell see it?
102+
zsh -i -c 'which gh'
103+
zsh -i -c 'which go'
104+
zsh -i -c 'which node'
105+
```
106+
107+
The `-i` flag forces an interactive shell, which sources `~/.zshrc`. If `zsh -c 'which gh'` prints
108+
`gh not found` but `zsh -i -c 'which gh'` prints the correct path, your PATH export is in
109+
`~/.zshrc` and you have confirmed the root cause.
110+
111+
<div className="note">
112+
<strong>Quick diagnosis:</strong> Run <code>zsh -c 'which gh'</code> (no <code>-i</code> flag). If this fails but <code>which gh</code> in your normal terminal works, your PATH is only set in <code>{"~/.zshrc"}</code>. Move the relevant exports to <code>{"~/.zshenv"}</code> to fix it.
113+
</div>
114+
115+
## The Fix: Move PATH Exports to ~/.zshenv
116+
117+
The solution is straightforward: any environment variable that must be visible to all processes — including non-interactive subshells — belongs in `~/.zshenv`, not `~/.zshrc`.
118+
119+
Open (or create) `~/.zshenv` and add your PATH exports there:
120+
121+
```bash
122+
# ~/.zshenv
123+
# Sourced for every zsh invocation — interactive, login, and non-interactive alike
124+
125+
# Homebrew (Apple Silicon)
126+
export PATH="/opt/homebrew/bin:/opt/homebrew/sbin:$PATH"
127+
128+
# Go
129+
export GOPATH="$HOME/go"
130+
export PATH="$GOPATH/bin:$PATH"
131+
132+
# Local binaries
133+
export PATH="$HOME/.local/bin:$PATH"
134+
```
135+
136+
For tools that inject themselves via an eval expression in `~/.zshrc` — such as nvm, pyenv, or rbenv — you need to move or duplicate that initialization into `~/.zshenv` as well:
137+
138+
```bash
139+
# ~/.zshenv — nvm initialization for non-interactive shells
140+
export NVM_DIR="$HOME/.nvm"
141+
[ -s "$NVM_DIR/nvm.sh" ] && source "$NVM_DIR/nvm.sh" --no-use
142+
143+
# pyenv
144+
export PYENV_ROOT="$HOME/.pyenv"
145+
export PATH="$PYENV_ROOT/bin:$PATH"
146+
eval "$(pyenv init --path)"
147+
```
148+
149+
Note the `--no-use` flag for nvm: it initializes nvm without switching to the default Node.js version, which speeds up shell startup for non-interactive contexts. Remove it if you want the default version active everywhere.
150+
151+
After saving `~/.zshenv`, verify without restarting your terminal:
152+
153+
```bash
154+
# Reload .zshenv in your current shell
155+
source ~/.zshenv
156+
157+
# Confirm Claude Code's shell type now finds the tool
158+
zsh -c 'which gh'
159+
zsh -c 'which go'
160+
zsh -c 'which node'
161+
```
162+
163+
## What to Keep in .zshrc vs .zshenv
164+
165+
Moving everything to `~/.zshenv` is not the right answer. Some configuration should stay in `~/.zshrc` because it only makes sense in interactive contexts or because it has side effects that slow down non-interactive shells unnecessarily.
166+
167+
<div className="tip">
168+
<strong>Guiding principle:</strong> If it is an environment variable that a program needs to find another program, it belongs in <code>~/.zshenv</code>. If it is a user-facing customization for your interactive terminal experience, it belongs in <code>~/.zshrc</code>.
169+
170+
<p><strong>Keep in <code>{"~/.zshenv"}</code>:</strong></p>
171+
<ul>
172+
<li><code>PATH</code> exports and modifications</li>
173+
<li><code>GOPATH</code>, <code>JAVA_HOME</code>, <code>PYTHONPATH</code>, <code>CARGO_HOME</code>, and similar tool-specific env vars</li>
174+
<li><code>NVM_DIR</code>, <code>PYENV_ROOT</code>, <code>RBENV_ROOT</code> and their <code>PATH</code> injections</li>
175+
<li><code>EDITOR</code>, <code>PAGER</code>, <code>LANG</code>, <code>LC_ALL</code></li>
176+
</ul>
177+
178+
<p><strong>Keep in <code>{"~/.zshrc"}</code>:</strong></p>
179+
<ul>
180+
<li>Shell aliases (<code>alias ll='ls -la'</code>)</li>
181+
<li>Prompt configuration (Starship, Powerlevel10k, oh-my-zsh)</li>
182+
<li>Tab completion setup</li>
183+
<li>Shell functions for interactive use</li>
184+
<li>History settings</li>
185+
<li><code>zsh</code> plugins and plugin managers</li>
186+
<li>Anything that prints output (welcome messages, <code>neofetch</code>, etc.)</li>
187+
</ul>
188+
</div>
189+
190+
## The Broader Pattern: Any Tool That Spawns Subshells
191+
192+
Claude Code is not unique here. This same behavior affects any process that spawns a child shell without the `-i` or `-l` flags:
193+
194+
- CI/CD pipelines (GitHub Actions, GitLab CI) run commands in non-interactive shells. This is why you often see pipelines that explicitly `source ~/.bashrc` or set up PATH at the top of every job.
195+
- Cron jobs run in minimal environments with almost no PATH set.
196+
- VS Code integrated terminal tasks and `launch.json` configurations may use non-interactive shells depending on the operating system and configuration.
197+
- SSH remote command execution (`ssh host 'command'`) uses a non-interactive shell unless you pass `-t` to force a TTY.
198+
- Make and other build systems that shell out to run commands.
199+
- Docker `RUN` instructions in Dockerfiles.
200+
201+
If you have ever fixed a "works on my machine" problem by adding `export PATH=...` to a CI configuration or a Dockerfile, you have already solved the same class of problem. The `~/.zshenv` fix is just the developer workstation equivalent.
202+
203+
<Blockquote
204+
quote="If a command works in your terminal but fails in a script, a CI job, or an AI agent, the first question to ask is: which startup files does this shell read?"
205+
/>
206+
207+
<KanvasCTA />
208+
209+
## Putting It Together: A Minimal ~/.zshenv Template
210+
211+
Here is a starting point for a `~/.zshenv` that covers the most common developer tools on macOS. Adjust paths to match your actual installations:
212+
213+
```bash
214+
# ~/.zshenv
215+
# Sourced for ALL zsh shells — interactive, login, and non-interactive.
216+
# Keep this file fast and free of output-producing commands.
217+
218+
# Homebrew (Apple Silicon Mac — change to /usr/local for Intel)
219+
if [[ -x /opt/homebrew/bin/brew ]]; then
220+
eval "$(/opt/homebrew/bin/brew shellenv)"
221+
fi
222+
223+
# Go
224+
export GOPATH="$HOME/go"
225+
export PATH="$GOPATH/bin:$PATH"
226+
227+
# Rust / Cargo
228+
export PATH="$HOME/.cargo/bin:$PATH"
229+
230+
# Local user binaries
231+
export PATH="$HOME/.local/bin:$PATH"
232+
233+
# nvm (initialize without switching to default version)
234+
export NVM_DIR="$HOME/.nvm"
235+
[ -s "$NVM_DIR/nvm.sh" ] && source "$NVM_DIR/nvm.sh" --no-use
236+
237+
# pyenv
238+
if command -v pyenv &>/dev/null || [[ -d "$HOME/.pyenv" ]]; then
239+
export PYENV_ROOT="$HOME/.pyenv"
240+
export PATH="$PYENV_ROOT/bin:$PATH"
241+
eval "$(pyenv init --path)"
242+
fi
243+
244+
# Editor and locale
245+
export EDITOR="vim"
246+
export LANG="en_US.UTF-8"
247+
export LC_ALL="en_US.UTF-8"
248+
```
249+
250+
With this in place, restart Claude Code (or any tool that spawns subshells) and run your verification:
251+
252+
```bash
253+
zsh -c 'which gh && which go && which node'
254+
```
255+
256+
All three should now resolve to their correct paths.
257+
258+
## Summary
259+
260+
The "command not found" error in Claude Code and similar AI coding assistants is a shell startup file problem, not a tool installation problem. Zsh only sources `~/.zshenv` for non-interactive, non-login shells. Everything most developers have placed in `~/.zshrc` — including PATH exports, version manager initializations, and tool-specific environment variables — is invisible to those shells.
261+
262+
The fix is permanent and simple:
263+
264+
1. Move PATH exports and tool-specific environment variables to `~/.zshenv`.
265+
2. Verify with `zsh -c 'which <tool>'` before and after.
266+
3. Keep interactive customizations (aliases, prompt, completions) in `~/.zshrc`.
267+
268+
The same fix benefits CI pipelines, cron jobs, Makefiles, Docker builds, and any other context where commands run in a non-interactive shell environment.
269+
270+
---
271+
272+
*Exploring AI-assisted development workflows and developer tooling? The <Link to="/community">Layer5 community</Link> is an active group of platform engineers, open source contributors, and DevOps practitioners. Join us on [Slack](https://slack.layer5.io) to share what you are building and get help when you hit walls like this one. You can also follow the <Link to="/blog">Layer5 blog</Link> for more practical engineering posts.*
273+
274+
</BlogWrapper>

0 commit comments

Comments
 (0)