ajwood
ajwood

Reputation: 19027

Setting environment variables in .screenrc

I'm trying to write a .screenrc file that I can use to set up for a developing on a particular project - It will start a few screens, cd to the right places, open up the right files in an editor, and set some environment variables needed for testing.

I can't get the environment setup to work.. I've tried putting this in `~/.screenrc:

setenv PATH ~/src/my_proj/bin/:$PATH

This doesn't work, and I think the problem is that after screen sets PATH, the regular shell initialization scripts kick in and change it on me.

I don't want to disable the regular shell init scripts. Is there any way to make screen setenv after the shell is initialized? Or alternatively, can screen set a variable to read-only?

Upvotes: 11

Views: 8718

Answers (4)

Mohanson
Mohanson

Reputation: 101

For me, I just set environment in ~/.bash_profile or ~/.bashrc

case $TERM in
screen*)
  export PS1='[\u:screen \w]\$ '
  ;;
*)
  export PS1='[\u \w]\$ '
  ;;
esac

It worked, enjoy it.

Upvotes: 1

Stephan Richter
Stephan Richter

Reputation: 1156

for me the line

setenv PATH /home/someuser/bin:$PATH

in the screenrc file did the trick.

I think the expansion of '~' to '/home/someuser' is bash specific and will not work within the screenrc.

Upvotes: 5

Nathan S. Watson-Haigh
Nathan S. Watson-Haigh

Reputation: 5123

I'd do it with some bash magic instead. Try adding something like this to your ~/.screenrc file:

screen -t "window" bash -ic 'PATH=~/src/my_proj/bin/:$PATH bash'

For more details and to have this set for newly created windows using Ctrl-a Ctrl+c or Ctrl-a c see my answer to a different post: https://stackoverflow.com/a/21717641/1413849

Upvotes: 6

Keith Thompson
Keith Thompson

Reputation: 263307

There's no way that screen can change the environment variables of a shell process once that process has launched, nor is there any way to make an environment variable read-only (values are stored in each process's memory, and each process has full access to them).

(Well, there might be some ugly system-specific way to do it, but it's the kind of thing Unix-like systems are designed to keep you from doing.)

You'll need to modify your shell's initialization script so that it retains the existing value of $PATH, possibly adding to it, rather than setting it to some new value ignoring its existing value.

If you want to do this conditionally, you can test for the existence of $STY, which is set only if the shell (or any other process) is running under screen.

Also, screen's setenv command doesn't seem to recognize the ~ character. I tried adding a similar setenv to a temporary screenrc, and $PATH contained a literal ~ character. bash seems to recognize the ~ syntax in $PATH, but other shells do not. Replace the ~ by $HOME, which screen does recognize.

Upvotes: 3

Related Questions