Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'd like to create a script that sets the $PATH to a set of directories chosen by me (thus ignoring the predefined paths, such as /usr/bin, /usr/local/bin and so on...) followed by launching ZSH.

This does the trick for BASH:

# exec /bin/bash --noprofile --norc

I get a new BASH session with the path being only what is defined above that I cancel with Ctrl-D when done. I'd like to recreate this script for ZSH but have had no success. I tried this:

# exec /bin/zsh -f

However the predefined paths such as /usr/bin and all that jazz remains which makes the script useless.

Preferably I'd like to have my .zshrc loaded and merely the path temporarily changed.

EDIT I forgot to add one thing - /etc/zshenv that is read first of all (or so I believe) contains this script:

# system-wide environment settings for zsh(1)
if [ -x /usr/libexec/path_helper ]; then
    eval `/usr/libexec/path_helper -s`

EDIT 2 The first edit is irrelevant as I don't want to use the -f option. A more precise question is:

"How do I add paths to precede the .zshrc paths to $PATH in a shell script followed by launching ZSH normally (as in with it reading .zshrc)?"

share|improve this question

Unfortunately, zsh always reads the global zshenv file, no matter how hard you tell it not to (i.e. even when you call zsh -f +d).

You can make a copy of the zsh executable, replacing the string /etc/zshenv by /NO!/zshenv.

Instead of telling zsh not to load user configuration files, you can specify your own, and undo the damage done by /etc/zshenv in $ZDOTDIR/.zshenv.

export ZDOTDIR="$(mktemp -d)"
cat <<'EOF' >"$ZDOTDIR/.zshenv"
rm "$ZDOTDIR/.zshenv"
rmdir "$ZDOTDIR"
share|improve this answer
I dabbled around for a while and realized that the script for BASH doesn't replace but simply append the directory to the front of $PATH (which is preferable) and this is what I would like to do but with ZSH. I just don't understand why the 'export PATH="/junk/and/stuff"' isn't added to the front of the path if I launch ZSH without the -f option. – totte Jun 14 '11 at 20:10
@Thoht: PATH=/junk/and/stuff sets the path, forgetting what's there. PATH=/junk/and/stuff:$PATH prepends your stuff to the path. If you're seeing stuff in $PATH that doesn't come from you, it's because you have a .bashrc that's doing the same kind of things as that zshenv (perhaps not exactly the same thing, so that it doesn't bother you in your particular application). – Gilles Jun 15 '11 at 8:06
I attempted this, '.zshrc' had 'export PATH="$PATH:/normal/paths/here"' and the script had 'export PATH="/junk/and/stuff:$PATH"' yet when I launched ZSH the path was set to '/normal/paths/here:/normal/paths/here' and the script had, to my knowledge, no effect. – totte Jun 15 '11 at 14:05
@Thoht: That would depend on the contents of the system files (/etc/zsh*). I don't know what OSX puts in there. – Gilles Jun 15 '11 at 15:00

Whoah, I solved it. I have little to no experience in shell scripting and ZSH, hence the confusing question. Allow me to rephrase it:

"How should I write a script that adds a bunch of directories to the front of $PATH and then runs ZSH?"

The solution:

export SOMETHING_PATH="/junk/and/stuff:"
exec /bin/zsh

The .zshrc

export PATH="$SOMETHING_PATH/bin:/usr/local/bin:/the/rest/of/your/paths"
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .