You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You type <code>gh pr list</code> in your terminal and it works perfectly. You ask Claude Code to
33
-
do the same, and it replies: <em>"command not found: gh"</em>. Your Go toolchain, nvm, pyenv,
34
-
Homebrew binaries — all invisible to the agent. This is not a bug in Claude Code. It is a
35
-
fundamental property of how Unix shells start up, and once you understand it, the fix is a
36
-
one-time five-minute change.
31
+
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.
37
32
</p>
38
33
</div>
39
34
40
35
## The Surprise
41
36
42
-
AI coding assistants like Claude Code, Cline, Aider, and similar tools spawn child shell processes
43
-
to run commands on your behalf. From where you sit, the terminal looks identical to the one you use
44
-
every day. But the shell those tools launch is fundamentally different from the one you interact
45
-
with — and that difference determines which startup files are read, which means it determines what
46
-
is on your PATH.
37
+
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.
47
38
48
-
The result is a confusing experience: tools you have used for years are suddenly invisible to the
49
-
agent trying to help you. The culprit is not your installation. It is the shell startup file
50
-
hierarchy.
39
+
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.
51
40
52
41
## Zsh Startup Files and When They Are Sourced
53
42
54
-
Zsh loads different configuration files depending on how it was launched. There are four main files,
55
-
and they are read in this order:
43
+
Zsh loads different configuration files depending on how it was launched. There are four main files, and they are read in this order:
@@ -61,25 +49,16 @@ and they are read in this order:
61
49
|`~/.zshrc`| No | Yes | No |
62
50
|`~/.zlogin`| Yes | No | No |
63
51
64
-
The key column is the last one. A **non-interactive, non-login shell** — the kind that Claude Code
65
-
spawns — sources **only**`~/.zshenv`. Everything else is skipped entirely.
66
-
67
-
-**`~/.zshenv`** is sourced for every zsh invocation, no matter what. It is the right place for
68
-
environment variables that must be available universally: `PATH`, `GOPATH`, `JAVA_HOME`, and
69
-
similar exports.
70
-
-**`~/.zprofile`** is sourced for login shells (e.g., when you open a new terminal window or SSH
71
-
into a machine). Homebrew places its environment setup here on Apple Silicon Macs
72
-
(`/opt/homebrew/bin/brew shellenv`), which is why Homebrew tools can also go missing.
73
-
-**`~/.zshrc`** is sourced only for interactive shells — sessions where you type commands. This is
74
-
where most developers put everything: aliases, prompt configuration, `nvm`, `pyenv`, `rbenv`,
52
+
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.
53
+
54
+
-**`~/.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.
55
+
-**`~/.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.
56
+
-**`~/.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`,
75
57
completions, and — critically — PATH customizations.
76
-
-**`~/.zlogin`** is sourced after `~/.zshrc` for login shells. It is rarely used by developers
77
-
directly.
58
+
-**`~/.zlogin`** is sourced after `~/.zshrc` for login shells. It is rarely used by developers directly.
78
59
79
60
<Blockquote
80
61
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."
81
-
person="Zsh Documentation"
82
-
title="Shell Startup File Hierarchy"
83
62
/>
84
63
85
64
## What PATH a Non-Interactive Shell Actually Sees
@@ -130,16 +109,13 @@ The `-i` flag forces an interactive shell, which sources `~/.zshrc`. If `zsh -c
130
109
`gh not found` but `zsh -i -c 'which gh'` prints the correct path, your PATH export is in
131
110
`~/.zshrc` and you have confirmed the root cause.
132
111
133
-
<divclass="note">
134
-
<strong>Quick diagnosis:</strong> Run <code>zsh -c 'which gh'</code> (no <code>-i</code> flag).
135
-
If this fails but <code>which gh</code> in your normal terminal works, your PATH is only set in
136
-
<code>~/.zshrc</code>. Move the relevant exports to <code>~/.zshenv</code> to fix it.
112
+
<divclassName="note">
113
+
<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.
137
114
</div>
138
115
139
116
## The Fix: Move PATH Exports to ~/.zshenv
140
117
141
-
The solution is straightforward: any environment variable that must be visible to all processes —
142
-
including non-interactive subshells — belongs in `~/.zshenv`, not `~/.zshrc`.
118
+
The solution is straightforward: any environment variable that must be visible to all processes — including non-interactive subshells — belongs in `~/.zshenv`, not `~/.zshrc`.
143
119
144
120
Open (or create) `~/.zshenv` and add your PATH exports there:
For tools that inject themselves via an eval expression in `~/.zshrc` — such as nvm, pyenv, or
162
-
rbenv — you need to move or duplicate that initialization into `~/.zshenv` as well:
137
+
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:
163
138
164
139
```bash
165
140
# ~/.zshenv — nvm initialization for non-interactive shells
Note the `--no-use` flag for nvm: it initializes nvm without switching to the default Node.js
176
-
version, which speeds up shell startup for non-interactive contexts. Remove it if you want the
177
-
default version active everywhere.
150
+
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.
178
151
179
152
After saving `~/.zshenv`, verify without restarting your terminal:
180
153
@@ -190,61 +163,57 @@ zsh -c 'which node'
190
163
191
164
## What to Keep in .zshrc vs .zshenv
192
165
193
-
Moving everything to `~/.zshenv` is not the right answer. Some configuration should stay in
194
-
`~/.zshrc` because it only makes sense in interactive contexts or because it has side effects
195
-
that slow down non-interactive shells unnecessarily.
196
-
197
-
<divclass="tip">
198
-
<strong>Guiding principle:</strong> If it is an environment variable that a program needs to
199
-
find another program, it belongs in <code>~/.zshenv</code>. If it is a user-facing customization
200
-
for your interactive terminal experience, it belongs in <code>~/.zshrc</code>.
201
-
202
-
**Keep in `~/.zshenv`:**
203
-
-`PATH` exports and modifications
204
-
-`GOPATH`, `JAVA_HOME`, `PYTHONPATH`, `CARGO_HOME`, and similar tool-specific env vars
205
-
-`NVM_DIR`, `PYENV_ROOT`, `RBENV_ROOT` and their `PATH` injections
- Anything that prints output (welcome messages, `neofetch`, etc.)
166
+
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.
167
+
168
+
<divclassName="tip">
169
+
<p>
170
+
<strong>Guiding principle:</strong> If it is an environment variable that a program needs to
171
+
find another program, it belongs in <code>{"~/.zshenv"}</code>. If it is a user-facing customization
172
+
for your interactive terminal experience, it belongs in <code>{"~/.zshrc"}</code>.
173
+
</p>
174
+
175
+
<p><strong>Keep in <code>{"~/.zshenv"}</code>:</strong></p>
176
+
<ul>
177
+
<li><code>PATH</code> exports and modifications</li>
178
+
<li><code>GOPATH</code>, <code>JAVA_HOME</code>, <code>PYTHONPATH</code>, <code>CARGO_HOME</code>, and similar tool-specific env vars</li>
179
+
<li><code>NVM_DIR</code>, <code>PYENV_ROOT</code>, <code>RBENV_ROOT</code> and their <code>PATH</code> injections</li>
<li><code>zsh</code> plugins and plugin managers</li>
191
+
<li>Anything that prints output (welcome messages, <code>neofetch</code>, etc.)</li>
192
+
</ul>
216
193
</div>
217
194
218
195
## The Broader Pattern: Any Tool That Spawns Subshells
219
196
220
-
Claude Code is not unique here. This same behavior affects any process that spawns a child shell
221
-
without the `-i` or `-l` flags:
197
+
Claude Code is not unique here. This same behavior affects any process that spawns a child shell without the `-i` or `-l` flags:
222
198
223
-
-**CI/CD pipelines** (GitHub Actions, GitLab CI) run commands in non-interactive shells. This is
224
-
why you often see pipelines that explicitly `source ~/.bashrc` or set up PATH at the top of
225
-
every job.
226
-
-**Cron jobs** run in minimal environments with almost no PATH set.
227
-
-**VS Code integrated terminal tasks** and `launch.json` configurations may use non-interactive
228
-
shells depending on the operating system and configuration.
229
-
-**SSH remote command execution** (`ssh host 'command'`) uses a non-interactive shell unless you
230
-
pass `-t` to force a TTY.
231
-
-**Make** and other build systems that shell out to run commands.
232
-
-**Docker `RUN` instructions** in Dockerfiles.
199
+
- 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.
200
+
- Cron jobs run in minimal environments with almost no PATH set.
201
+
- VS Code integrated terminal tasks and `launch.json` configurations may use non-interactive shells depending on the operating system and configuration.
202
+
- SSH remote command execution (`ssh host 'command'`) uses a non-interactive shell unless you pass `-t` to force a TTY.
203
+
- Make and other build systems that shell out to run commands.
204
+
- Docker `RUN` instructions in Dockerfiles.
233
205
234
-
If you have ever fixed a "works on my machine" problem by adding `export PATH=...` to a CI
235
-
configuration or a Dockerfile, you have already solved the same class of problem. The `~/.zshenv`
236
-
fix is just the developer workstation equivalent.
206
+
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.
237
207
238
208
<Blockquote
239
209
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?"
240
-
person="Platform Engineering"
241
-
title="Debugging Shell Environment Issues"
242
210
/>
243
211
212
+
<KanvasCTA />
213
+
244
214
## Putting It Together: A Minimal ~/.zshenv Template
245
215
246
-
Here is a starting point for a `~/.zshenv` that covers the most common developer tools on macOS.
247
-
Adjust paths to match your actual installations:
216
+
Here is a starting point for a `~/.zshenv` that covers the most common developer tools on macOS. Adjust paths to match your actual installations:
248
217
249
218
```bash
250
219
# ~/.zshenv
@@ -283,8 +252,7 @@ export LANG="en_US.UTF-8"
283
252
export LC_ALL="en_US.UTF-8"
284
253
```
285
254
286
-
With this in place, restart Claude Code (or any tool that spawns subshells) and run your
287
-
verification:
255
+
With this in place, restart Claude Code (or any tool that spawns subshells) and run your verification:
288
256
289
257
```bash
290
258
zsh -c 'which gh && which go && which node'
@@ -294,33 +262,18 @@ All three should now resolve to their correct paths.
294
262
295
263
## Summary
296
264
297
-
The "command not found" error in Claude Code and similar AI coding assistants is a shell startup
298
-
file problem, not a tool installation problem. Zsh only sources `~/.zshenv` for non-interactive,
299
-
non-login shells. Everything most developers have placed in `~/.zshrc` — including PATH exports,
300
-
version manager initializations, and tool-specific environment variables — is invisible to those
301
-
shells.
265
+
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.
302
266
303
267
The fix is permanent and simple:
304
268
305
269
1. Move PATH exports and tool-specific environment variables to `~/.zshenv`.
306
270
2. Verify with `zsh -c 'which <tool>'` before and after.
307
271
3. Keep interactive customizations (aliases, prompt, completions) in `~/.zshrc`.
308
272
309
-
The same fix benefits CI pipelines, cron jobs, Makefiles, Docker builds, and any other context
310
-
where commands run in a non-interactive shell environment.
273
+
The same fix benefits CI pipelines, cron jobs, Makefiles, Docker builds, and any other context where commands run in a non-interactive shell environment.
311
274
312
275
---
313
276
314
277
*Exploring AI-assisted development workflows and developer tooling? The <Linkto="/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 <Linkto="/blog">Layer5 blog</Link> for more practical engineering posts.*
315
278
316
-
<CTA_FullWidth
317
-
image={CTAImg}
318
-
heading="Join the Layer5 Community"
319
-
alt="Layer5 Community"
320
-
content="Connect with platform engineers, DevOps practitioners, and open source contributors who are building the future of cloud native infrastructure."
0 commit comments