From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 22399 invoked by alias); 15 Dec 2012 19:19:53 -0000 Mailing-List: contact zsh-users-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Users List List-Post: List-Help: X-Seq: 17477 Received: (qmail 15846 invoked from network); 15 Dec 2012 19:19:51 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.2 Received-SPF: none (ns1.primenet.com.au: domain at closedmail.com does not designate permitted sender hosts) From: Bart Schaefer Message-id: <121215111934.ZM32088@torch.brasslantern.com> Date: Sat, 15 Dec 2012 11:19:34 -0800 In-reply-to: Comments: In reply to Florian Lindner "Correct way to set environment" (Dec 15, 2:29pm) References: X-Mailer: OpenZMail Classic (0.9.2 24April2005) To: zsh-users@zsh.org Subject: Re: Correct way to set environment MIME-version: 1.0 Content-type: text/plain; charset=us-ascii On Dec 15, 2:29pm, Florian Lindner wrote: } } I've ushed .zshenv for that purpose since it is sourced on every shell } invocation. So far so good. } 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. You do need to export any variable that you want passed down to child processes of the shell, such as the python interpreter. PATH is one of the variables exported by default, but PYTHONPATH is not. I think you'll note that PATH is similarly getting longer every time. The solution to this is to test whether the value already contains the substring you're going to add, and skip the assignment if it would be redundant. You can do this yourself -- export PYTHONPATH if [[ $PYTHONPATH != $HOME/flof/src:* ]]; then PYTHONPATH=$HOME/flof/src:$PYTHONPATH fi -- or you can let zsh do it by declaring the variable to be an array containing unique values. At startup, zsh "ties" the array variable $path to the environment string $PATH. If you run print $path print $PATH you should see strings that are identical except that the first one has spaces where the second has colons. (If instead you see only the first element of $PATH when printing $path, you have the ksharrays option set, and must use ${path[@]} instead.) With this connection, anything assigned to path is copied to PATH and vice-versa, so now you can do: path=(~/flof/src $path) You can set up a similar equivalence yourself with the typeset command: typeset -T PYTHONPATH pythonpath pythonpath=(~/flof/src $pythonpath) With this is place, you can declare the array values to be unique: typeset -U path pythonpath Zsh then collapses duplicates out of the arrays and updates the "tied" strings to match. Note, however, that if you assign directly to the string variable instead of assigning to the array, then zsh does NOT apply the uniqueness property, so you must remember to use the array assignment form after tying. There is one exception to the above: If you combine creating the "tie" along with the assignment into a single command, the uniqueness property applies. typeset -UTx PYTHONPATH=$HOME/flof/src:$PYTHONPATH pythonpath You can also use export -UT PYTHONPATH=$HOME/flof/src:$PYTHONPATH pythonpath if you think that reads better. This doesn't work for $path because it's already "special", so you must fix that one up in two steps. path=(~/flof/src $path) typeset -U path Enjoy.