3

I'm trying to move from screen to tmux (to eventually using tmux from within byobu). However, I have a severe problem with environment variables not being applied, especially PS1. None of them get copied to the session (or window) environments. I found this thread that seemed relevant:

How do I start tmux with my current environment?

However, I actually can see all my right variables when I do

:show-environment -g

But none of them get carried over to the session environment, so I don't see anything when I do

:show-environment

I do have the right update-environment statement in my ~/.tmuxrc file:

# start a non-login shell by default for each new window
set -g default-command 'bash'

# Prefix is C-a like in screen
unbind C-b
set -g prefix C-a

# Carry over the PS1
set-option -ga update-environment "PS1"

Frankly this all seems like a mess to me. I can see the benefit of starting from a clean session for each screen, but for the most part this seems like a pain. I export the variable I intend to use in sub-processes, such as as the Python virtualenvwrapper functions, and expect them to be available.

Is there a way to disable this behavior? If not, what should I try to carry over my PS1 variable to tmux?

EDIT 11/13/2013

I realized that removing the first line (default-command bash) does carry over all of the environment variables. However, I really don't want each new screen of tmux to launch as a login shell. For instance, I specifically declared my PS1 variable in a login shell, so that it wouldn't be overwritten whenever I open a new screen.

Based on the following post, every new screen in tmux should launch as a non-login shell: https://superuser.com/questions/614277/profile-and-bash-profile-ignored-when-starting-tmux-from-bashrc

Why is this not happening by default for me?

Community
  • 1
  • 1
user1496984
  • 10,957
  • 8
  • 37
  • 46

1 Answers1

6

Tmux cannot update running processes (e.g. bash), it can only update its own environment. If you were to start a new window/pane it would pick up the new environment. My suggestion would be to use a utility function like this:

#!/bin/bash

tmup () 
{ 
    echo -n "Updating to latest tmux environment...";
    export IFS=",";
    for line in $(tmux showenv -t $(tmux display -p "#S") | tr "\n" ",");
    do
        if [[ $line == -* ]]; then
            unset $(echo $line | cut -c2-);
        else
            export $line;
        fi;
    done;
    unset IFS;
    echo "Done"
}

This goes through all the updated environment that tmux has learned about and updates your shell.

sjbx
  • 1,205
  • 2
  • 12
  • 12
  • Thanks for the answer. I know tmux cannot update the env variables of running processes, but that's not what I want. I'd like the newly opened panes and windows to have the exact same set of variables that tmux had when it was launched. This seems to be the default behavior of screen, but I still don't see why it's so hard to emulate in tmux? – user1496984 Nov 06 '13 at 16:29
  • Ah, sorry I misunderstood. Surely what you want is achievable by removing everything from update-environment. You shouldn't use `-a` in `set-option` since this will append to `update-environment`. – sjbx Nov 07 '13 at 11:22
  • Thanks again for the pointer! I updated the question with some new findings. It was the first line that was causing the issue with the environment variables. However, without that line, each new screen in tmux launches a login shell for some reason. And I am not sure how to change this behavior.. – user1496984 Nov 13 '13 at 23:46