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

Re: loading user startup files for zsh scripts



On Feb 8,  7:37pm, Greg Klanderman wrote:
} Subject: Re: loading user startup files for zsh scripts
}
} 
} >>>>> On February 8, 2011 Peter Stephenson <Peter.Stephenson@xxxxxxx> wrote:
} 
} > You can test if [[ $0 == zsh ]].  If it is, either you're running a
} > script helpfully called zsh, or you're not running a script at all.
} 
} /bin/zsh -c 'echo $0'
} 
} prints '/bin/zsh' so that's not exactly right I guess.

I was going to suggest testing $0 also, though slightly differently.

    if [[ $ZSH_NAME == (|-)$0:t ]]
    then print This shell is unlikely to be reading a script
    else print This shell is almost certainly reading a script
    fi

The ZSH_NAME variable is always constructed from the tail of the path
name of the command interpreter, so if $0 is the same then you are not
running a script -- or the script has the same basename as the shell,
but then you're into the realm of deliberate tomfoolery.

} Isn't there some case where it can end up '-zsh' as well?

That normally happens only for login shells started by terminal manager
processes like getty (mingetty, etc.).  It can be deliberately be made
to happen by using zsh's ARGV0 environment variable, or by an executable
poking something into the arguments of execve() et al.

For your purposes, if a script were deliberately run as a login shell
(e.g., with "#!/bin/zsh -l"), wouldn't you want it to read ~/.zshenv?
In which case you can discard the (|-) that I threw in to that test.



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