Bash – Screen – inherit current window’s environment

bashenvironment-variablesgnu-screenshell

In GNU Screen, how can I create a new window whose shell inherits the environment of the shell in the current window?

The usual Ctrl+A C doesn't seem to do this.

Best Answer

If you've set environment variables on one screen (say running bash), and then open a new screen, it is a separate bash process and therefore will not pick up the environment on the separate already running bash shell. A quick fix to get around the issue would be:

env TERMCAP= env | sed -r 's/^(\w+)=(.*)$/\1="\2"/' > env.sh

then once you've Ctrlac to get a new shell you can then

source env.sh

It's hacky and I use env TERMCAP= env because the TERMCAP environment variable is multi-line and makes the sed far more complicated. It's not pretty, but it works :)

You may want to change it to do:

env TERMCAP= env | sed -r 's/^(\w+)=(.*)$/export \1="\2"/' > env.sh

So the variables are also exported.