Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Correct way to set environment

On Sat, 2012-12-15 at 14:29 +0100, Florian Lindner wrote:
> I'm a bit puzzled about the way to set global environment variables.
> I've ushed .zshenv for that purpose since it is sourced on every shell
> invocation.
> florian@horus ~ % cat .zshenv
> PATH=$HOME/flof/src:$HOME/software/bin:$PATH
> PATH=/home/florian/software/src/boar:$PATH
> No other relevant z-files are present. This works as far as it sets
> the PYTHONPATH variable but if I launch python it is not taken into
> account. When I use export PYTHONPATH, the pythonpath gets longer and
> longer if I invoke a zsh session within a zsh session.

First thing:  unless the variable is exported, it remains as a shell
variable and does not get into the environment.  You do need to export
the variable.

When you do this, as you have noticed, because .zshenv is sourced at
every subshell invocation, the path keeps growing because you keep
appending or prepending values to the path every time.

> What is the best way to set some environment variables, no matter how
> (login, interactive, ...) the shell is invoced?

You could use a guard environment variable:

        if [[ $MY_ENVIRONMENT != yes ]]
          export PATH=$HOME/flof/src:$HOME/software/bin:$PATH
          export PATH=/home/florian/software/src/boar:$PATH
          export PYTHONPATH=$HOME/flof/src:$PYTHONPATH
          export MY_ENVIRONMENT=yes

Or you could only append a path to these variables if they don't contain
it already.


Messages sorted by: Reverse Date, Date, Thread, Author