TMUX setting environment variables for sessions

后端 未结 4 1732
故里飘歌
故里飘歌 2021-02-04 00:07

I work in a situation where I have multiple projects and within each are many scripts that make use of environment variables set to values specific to that project.

What

相关标签:
4条回答
  • 2021-02-04 00:19

    You can access tmux (local) environment variables for each session, while in a session, with the command:

    bash> tmux show-environment
    

    If you add the -g parameter you get the environment for all sessions, i.e. the global environment. The local environments are NOT the same as the global environment. The previous command prints the entire local environment, but you can also look at just one variable:

    bash> tmux show-environment variable_name
              variable_name=value
    

    To get the value, you could use some 'sed' magic or use 'export' on a single variable, or you can even 'export' the entire environment to your shell. Below are the 3 approaches.

    bash> tmux show-environment variable_name | sed "s:^.*=::"
             value
    
    bash> eval "export $(tmux show-environment variable_name)"
    bash> echo $variable_name
             value    
    
    bash> for var in $(tmux show-environment | grep -v "^-"); do eval "export $var"; done;
    bash> echo $variable_name
             value    
    

    If needed, you can just add the -g parameter after the show-environment command if you want to access the global environment.

    0 讨论(0)
  • 2021-02-04 00:31

    I figured out a way to do this. I'm using tmux 2.5.

    Background

    In the tmux man page, it states that there are two groups of environment variables: global and per-session. When you create a new tmux session, it will merge the two groups together and that becomes the set of environment variables available within the session. If the environment variables get added to the global group, it appears that they get shared between all open sessions. You want to add them to the per-session group.

    Do this

    Step 1: Create a tmux session.

    tmux new-session -s one
    

    Step 2: Add an environment variable to the per-session group.

    tmux setenv FOO foo-one
    

    This adds the environment variable to per-session set of environment variables. If you type tmux showenv, you'll see it in the output. However, it isn't in the environment of the current session. Typing echo $FOO won't give you anything. There's probably a better way to do this, but I found it easiest to just export it manually:

    export FOO='foo-one'
    

    Step 3: Create new windows/panes

    Now, every time you create a new window or pane in the current session, tmux will grab the FOO environment variable from the per-session group.

    Automating it

    I use bash scripts to automatically create tmux sessions that make use of these environment variables. Here's an example of how I might automate the above:

    #!/bin/bash
    BAR='foo-one'
    tmux new-session -s one \; \
      setenv FOO $BAR \; \
      send-keys -t 0 "export FOO="$BAR C-m \; \
      split-window -v \; \
      send-keys -t 0 'echo $FOO' C-m \; \
      send-keys -t 1 'echo $FOO' C-m
    
    0 讨论(0)
  • 2021-02-04 00:38

    Example

    In my case I'm starting a tmux session per project within a bash script and I needed to activate a relevant virtual environment for that session, including any newly opened tabs. To do so, I added the following virtual environment activation code to the ~/.bashrc:

    if [ -n "$VIRTUAL_ENV" ]; then
        source $VIRTUAL_ENV/bin/activate;
    fi
    

    However if I need to set foo environment for Session_1 and bar environment for Session_2, the VIRTUAL_ENV variable is globally set to bar after creating Session_2, so any newly opened tabs in Session_1 erroneously ends up in bar environment.

    Solution

    original HOWTO (commit).

    1. Add the following in your ~/.profile (or ~/.bashrc):
    # For Tmux VirtualEnv support
    tmux_get_var(){
        local key=$1
        [[ -n "$TMUX" ]] && tmux showenv | awk -F= -v key="$key" '$1==key {print $2}'
    }
    
    # activate the virtual environment if it is declared
    venv=$(tmux_get_var "VIRTUAL_ENV")
    if [ -n "$venv" ]; then
        source $venv/bin/activate;
    fi
    
    1. Set the VIRTUAL_ENV variable for the target session:
    tmux setenv -t ${SESSION_NAME} 'VIRTUAL_ENV' /path/to/virtualenv/
    
    0 讨论(0)
  • 2021-02-04 00:42

    I've done a simple

    export MY_VAR="some value"
    

    before I start the tmux session, which gives me access to MY_VAR from all windows inside that session.

    0 讨论(0)
提交回复
热议问题