# terminal: Remove SHLVL from terminal environment to fix incorrect shell level \(\#44835\) · gitcafe/zed

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

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

Visibility: public

Requested revision: 6cab835003e5e083aa3ed8dc61223e0b2cc59026

Requested commit: 6cab835003e5e083aa3ed8dc61223e0b2cc59026

Commit: 6cab835003e5e083aa3ed8dc61223e0b2cc59026

Tree: 8c954739cddd21e4bbb81a984c15d5542a399881

Author: rari404

Committer: GitHub

## Message

```
terminal: Remove SHLVL from terminal environment to fix incorrect shell level (#44835)

Fixes #33958

## Problem

When opening a terminal in Zed, `SHLVL` incorrectly starts at 2 instead
of 1. On `workspace: reload`, it increases by 2 instead of 1.

## Root Cause

1. Zed's `shell_env::capture()` spawns a login shell (`-l -i -c`) to
capture the user's environment, which increments `SHLVL`
2. The captured `SHLVL` is passed through to the PTY options
3. When alacritty_terminal spawns the user's shell, it increments
`SHLVL` again

Result: `SHLVL` = captured value + 1 = 2 (when launched from Finder)

## Solution

Remove `SHLVL` from the environment in `TerminalBuilder::new()` before
passing it to alacritty_terminal. This allows the spawned shell to
initialize `SHLVL` to 1 on its own, matching the behavior of standalone
terminal emulators like iTerm2, Kitty, and Alacritty.

## Testing

- Launch Zed from Finder → open terminal → `echo $SHLVL` → should output
`1`
- Launch Zed from shell → open terminal → `echo $SHLVL` → should output
`1`
- `workspace: reload` → open terminal → `echo $SHLVL` → should remain
`1`
- Tested with bash, zsh, fish

Release Notes:

- Fixed terminal `$SHLVL` starting at 2 instead of 1
([#33958](https://github.com/zed-industries/zed/issues/33958))
```

## Parents

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

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