# terminal: Skip SHLVL when loading login shell environment \(\#46273\) · gitcafe/zed

[View on GitCafe](https://git.cafe/gitcafe/zed/commit/466c7008e049fd7431c073f053e58544032b3337)

Repository: [gitcafe/zed](https://git.cafe/gitcafe/zed)

Visibility: public

Requested revision: 466c7008e049fd7431c073f053e58544032b3337

Requested commit: 466c7008e049fd7431c073f053e58544032b3337

Commit: 466c7008e049fd7431c073f053e58544032b3337

Tree: 2f884584c02ca8c9a713fcc7161d89d4e82702ff

Author: rari404

Committer: GitHub

## Message

```
terminal: Skip SHLVL when loading login shell environment (#46273)

Fixes #33958

## Problem

PR #44835 attempted to fix SHLVL starting at 2 by removing it from the
`env` HashMap passed to alacritty. However, that HashMap is only an
**overlay** — alacritty uses the parent process environment as a base
and applies the overlay on top. Since alacritty never calls
`env_remove("SHLVL")`, the terminal shell still inherits `SHLVL` from
Zed's process environment.

## Root Cause

The real issue is in `load_login_shell_environment()`:

1. `shell_env::capture()` spawns a login shell to capture environment
variables
2. That login shell increments `SHLVL` (from 0→1 or n→n+1)
3. The captured env (including the incremented `SHLVL`) is written to
Zed's **process environment** via `env::set_var`
4. When you open a terminal, the shell inherits Zed's `SHLVL` and
increments it again → starts at 2
5. On reload, `shell_env::capture()` runs again with the
already-elevated `SHLVL`, incrementing it further → +2 per reload

## Fix

Skip `SHLVL` when setting Zed's process environment in
`load_login_shell_environment()`. This prevents the login-shell capture
from polluting Zed's env, so terminals start fresh with the correct
`SHLVL`.
```

## Parents

- [cfa478dde414e008138c1d8ec54c015ed3af47d6](https://git.cafe/gitcafe/zed/commit/cfa478dde414e008138c1d8ec54c015ed3af47d6?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zed/tree/466c7008e049fd7431c073f053e58544032b3337?format=markdown)
