From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 18205 invoked by alias); 9 Feb 2011 04:59:33 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 28721 Received: (qmail 23892 invoked from network); 9 Feb 2011 04:59:21 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) 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.1 Received-SPF: none (ns1.primenet.com.au: domain at closedmail.com does not designate permitted sender hosts) From: Bart Schaefer Message-id: <110208205856.ZM24066@torch.brasslantern.com> Date: Tue, 08 Feb 2011 20:58:56 -0800 In-reply-to: Comments: In reply to Greg Klanderman "Re: loading user startup files for zsh scripts" (Feb 8, 7:37pm) References: <19792.22365.139876.599478@gargle.gargle.HOWL> <110207213357.ZM22407@torch.brasslantern.com> <20110208172056.6a985c90@pwslap01u.europe.root.pri> X-Mailer: OpenZMail Classic (0.9.2 24April2005) To: zsh-workers@zsh.org Subject: Re: loading user startup files for zsh scripts MIME-version: 1.0 Content-type: text/plain; charset=us-ascii On Feb 8, 7:37pm, Greg Klanderman wrote: } Subject: Re: loading user startup files for zsh scripts } } } >>>>> On February 8, 2011 Peter Stephenson 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.