Marty
Marty

Reputation: 6644

Altering zsh's $path variable in a script that is to be sourced

I've inherited a zsh script which sets up a bunch of environment variables for some simulations. It wants to edit the $path variable so that some perl scripts can be found:

typeset -U path
path=( ${SIMENV_BIN} $path )

However, when I source the script (source setup.source) the $path variable remains untouched. If I copypaste these lines to the command prompt, they do update $path. If I sprinkle 'echo's in the script and source it again, the typeset command seems to clear the $path variable.

Would someone mind explaining to me what is going on, and how I can fix it?

Upvotes: 4

Views: 1150

Answers (2)

qqx
qqx

Reputation: 19465

By default the typeset command creates a new variable with the supplied name that is local to the current function, so changes to that variable will be lost when the function returns. Add the -g option:

typeset -gU path

This will prevent localizing the variable.

Upvotes: 3

Himanshu
Himanshu

Reputation: 482

I think you might need to use .zshenv since it is sourced on all invocations of the shell.

http://zsh.sunsite.dk/Contrib/startup/std/zshenv

Upvotes: 2

Related Questions