# CLAUDE_ENV_FILE

> A script that runs before each Bash command, so its exports stay.

**Field:** Artificial Intelligence  
**Category:** Claude Code  
**Also known as:** CLAUDE_ENV_FILE=~/.claude/env.sh

## Think of it like

It is like you put on your apron each time you cook.

## Definition

CLAUDE_ENV_FILE names a shell script that Claude Code runs before each Bash command, in the same shell. So anything the script exports, the command can see. It is the way to keep a Python virtualenv or a conda setup turned on across commands. Some hooks, such as SessionStart, can also write to it.

## Example

Jack's tests need his Python virtualenv. Each command forgot it. He puts the activate line in an env file and sets this.

## Example code

```bash
# env.sh
source ~/projects/app/.venv/bin/activate

export CLAUDE_ENV_FILE=~/env.sh
claude
```

Keep a Python env on for each command.

## Why it matters

Each Bash command starts fresh. Without this, a setup step does not carry to the next command.

## Common mistake

Putting the activate line in a normal command and expecting it to last. It is gone by the next one.

## Three principles

- **Runs before each command.** Same shell.
- **Exports carry.** Into the command.
- **Hooks can fill it.** SessionStart.

## Related terms

- [CLAUDE_CODE_SHELL_PREFIX](https://learn.benibix.com/term/ai/claude-code-shell-prefix-variable)
- [Bash Tool](https://learn.benibix.com/term/ai/bash-tool)
- [Environment Variable](https://learn.benibix.com/term/infrastructure/environment-variable)

## Told apart from

- [CLAUDE_ENV_FILE vs CLAUDE_CODE_SHELL_PREFIX](https://learn.benibix.com/compare/claude-env-file-variable-vs-claude-code-shell-prefix-variable): People think CLAUDE_ENV_FILE and the shell prefix both just run something before each command, so either will do. They work at different levels. The env file runs inside the same shell as the command, so what it exports, the command sees. The prefix is a program that wraps the whole command from outside. Use the env file to set up, and the prefix to log or check.

![How a Bash command is run: the prefix wraps the shell, the shell runs the env file, then the command.](https://learn.benibix.com/diagrams/env-bash-command-path.svg)

---

Source: https://learn.benibix.com/term/ai/claude-env-file-variable
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-16
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
