Screen Environment – How to Set Screen Environment Variable from Bash

environment-variablesgnu-screen

Background

I use virtualenv to switch between environments "1.0" and "2.0".
I use screen to manage terminals.
When I work on one environment I want all new screen windows to start with this environment.

Question

Can bash instruct screen to set environment variable on new shell instances, so I could make an alias similar to this one:

alias one='export PRJCT=1.0; screen-magic-setenv PRJCT=1.0; workon 1.0'
alias two='export PRJCT=2.0; screen-magic-setenv PRJCT=2.0; workon 2.0'

and afterwards in ~/.bashrc call:

[[ -z $PRJCT ]] || workon $PRJCT # switch to project if set

I know I can command screen to set environment variable on new shells like below but it doesn't help as it won't work in an alias:

:setenv PRJCT
2.0

Best Answer

If I understand correctly, your problem is you cannot find a way to use a shell alias to interact with screen directly. Instead, you can send commands to a running screen using -X, including setenv of course, e.g.:

$ screen -list
There are screens on:
    25216.pts-45.antiriad      (Attached)

$ screen -S 25216 -X setenv PRJCT 2.0

The variable is then set, and will be inherited by child shells from that point on, exactly as with :setenv. If you use -S to set sensible distinct session names your task may be easier. The variable STY holds the current screen session.

If you run just screen -X setenv PRJCT 2.0 within a screen session, omitting -S, it will apply to that screen instance.

alias one='export PRJCT=1.0; screen -X setenv PRJCT 1.0; workon 1.0'
Related Question