Tmux actually isolates your shell from the environment it was launched from, which is the opposite of what most people assume.

Let’s see what that means. Imagine you’re in your main shell, and you’ve set a custom environment variable:

export MY_CUSTOM_VAR="hello from parent"
echo $MY_CUSTOM_VAR
# Output: hello from parent

Now, you launch tmux:

tmux new-session -d 'echo $MY_CUSTOM_VAR'
# Output: (empty line)

See that? The tmux session doesn’t automatically inherit MY_CUSTOM_VAR. This is by design. tmux aims to provide a clean, predictable environment for your terminal multiplexing. If it inherited everything, stray variables from your parent shell could interfere with tmux’s own operations or the applications you run within it.

So, how do we get that variable into the tmux session? There are a few ways, each with a slightly different flavor.

Explicitly Passing Variables on Session Creation

The most direct way is to tell tmux exactly which variables to pass when you create a new session or window. You use the -e flag for this.

Let’s set our variable again:

export MY_CUSTOM_VAR="hello from parent"

Now, create a tmux session and pass it:

tmux new-session -d -e MY_CUSTOM_VAR 'echo $MY_CUSTOM_VAR'
tmux capture-pane -pS '' # Capture output of the command
# Output: hello from parent

This works because tmux explicitly injects MY_CUSTOM_VAR into the environment of the shell that runs the command within that new pane.

You can also pass multiple variables:

export ANOTHER_VAR="world"
tmux new-session -d -e MY_CUSTOM_VAR -e ANOTHER_VAR 'echo "$MY_CUSTOM_VAR $ANOTHER_VAR"'
tmux capture-pane -pS ''
# Output: hello from parent world

Setting Variables for All New Sessions

If you find yourself constantly passing the same variables, you can configure tmux to do it automatically for all new sessions. This is done in your ~/.tmux.conf file.

Add this line to your ~/.tmux.conf:

set-environment -g MY_PERSISTENT_VAR "persistent value"

After reloading your tmux configuration (either by tmux source-file ~/.tmux.conf or by restarting tmux), any new session you create will have MY_PERSISTENT_VAR available:

tmux new-session -d 'echo $MY_PERSISTENT_VAR'
tmux capture-pane -pS ''
# Output: persistent value

The -g flag makes this a global setting, meaning it applies to all sessions managed by this tmux server.

Passing Variables to Existing Sessions

What if you’re already inside a tmux session and realize you need a variable that wasn’t passed? You can use the set-environment command within tmux.

Start a tmux session:

tmux

Now, inside the tmux pane, set the variable:

tmux set-environment MY_NEW_VAR "set inside tmux"

This command sets the environment variable for the current tmux server. Any new pane or window you create after running this command will have MY_NEW_VAR available.

Let’s test this. Create a new window within your existing tmux session:

tmux new-window 'echo $MY_NEW_VAR'

The output in the new window will be:

set inside tmux

However, if you try to echo $MY_NEW_VAR in the original pane where you ran set-environment, it won’t be there:

echo $MY_NEW_VAR
# Output: (empty line)

This is because set-environment affects the tmux server’s environment, which then propagates to newly created shells/panes. It doesn’t retroactively inject into already running shells. To get it into an existing shell, you’d typically need to source a profile or re-run the export command in that specific shell.

The remain-on-exit Trick for Command Output

This is a bit more advanced and less about direct environment variable passing, but it’s related to capturing output from commands that run and exit. If you have a command that sets an environment variable and then exits, tmux might discard the output. You can use remain-on-exit on to keep the pane open.

Consider a script set_and_exit.sh:

#!/bin/bash
export MY_SCRIPT_VAR="from script"
echo "Variable set: $MY_SCRIPT_VAR"
exit 0

If you run this directly in tmux:

tmux new-window './set_and_exit.sh'
# Output in new window: Variable set: from script (pane then closes)

The pane closes immediately. But if you want to use that variable in a subsequent command in the same pane, you need the environment to persist. The remain-on-exit option helps here.

tmux new-window 'set -g remain-on-exit on; ./set_and_exit.sh; echo "Now in new shell..."'

After this command runs, the pane will stay open, and the MY_SCRIPT_VAR might be available in the shell that remain-on-exit keeps alive. However, tmux’s set-environment is generally cleaner for managing variables across sessions.

The most surprising thing about tmux environment variables is how it acts as a barrier, not a conduit, to your parent shell’s environment. This isolation is crucial for stability but requires explicit configuration to pass necessary context.

If you’re using ssh within tmux and encountering issues with remote environment variables not being available, you’ll next want to look into ssh-agent forwarding and tmux’s forward-agent option.

Want structured learning?

Take the full Tmux course →