Linux – Updating screen session environment variables to reflect new graphical login

dbusenvironment-variablesgnu-screenlinuxxorg

I use linux, and I like to do all my command-line work within a single screen session, so that I can restart my graphical login and such without losing my terminals. However, when I log out and back into my graphical session, this changes all my session environment variables, such as DBus sessions. This means that after logging in again, my screen session now has the old (and wrong) environment variables. So now when I try to start graphical programs from my screen session, at best they emit a warning about not being able to connect to the session bus. At worst, they fail to start completely.

So, what I'm looking for is a way to modify environment variables in a running instance of screen, so that all subsequently-created screen windows will inherit the new environment variables. Is there a way to do this?

Best Answer

You cannot start a shell script from the screen session since it would inherit the old environment. You can however us a fifo to get the new environment variables into the old screen session. You can fill that fifo when you start your graphical session.

#!/bin/bash
FIFO=/tmp/your_variables
[ -e $FIFO ] && cat $FIFO > /dev/null || mkfifo $FIFO

# save number of variables that follow
NVARS=2
echo $NVARS > $FIFO
echo ENV1=sth1 > $FIFO
echo ENV2=sth2 > $FIFO

Start that script in the background on login (it will only terminate when all variables are read from it).

Now you can read from the fifo, e.g. add this function to your .bashrc

update_session() {
  FIFO=/tmp/your_variables

  NVAR=$(cat $FIFO)
  for i in $(seq $NVAR); do
    export $(cat $FIFO)
  done
  #delete the pipe, or it will not work next time 
  rm $FIFO
}

so that you can in your old screen session

update_session
Related Question