zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: zsh-workers@zsh.org
Subject: Re: loading user startup files for zsh scripts
Date: Tue, 08 Feb 2011 20:58:56 -0800	[thread overview]
Message-ID: <110208205856.ZM24066@torch.brasslantern.com> (raw)
In-Reply-To: <m362sudov6.fsf@klanderman.net>

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@csr.com> 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.


  reply	other threads:[~2011-02-09  4:59 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-02-07 20:34 Greg Klanderman
2011-02-08  5:33 ` Bart Schaefer
2011-02-08 17:09   ` Greg Klanderman
2011-02-08 17:20     ` Peter Stephenson
2011-02-09  0:37       ` Greg Klanderman
2011-02-09  4:58         ` Bart Schaefer [this message]
2011-02-09 15:32           ` Greg Klanderman
2011-02-09 16:01           ` Greg Klanderman
2011-02-09 16:53             ` Bart Schaefer
2011-02-10 16:30               ` Greg Klanderman
2011-02-10 17:52                 ` Bart Schaefer
2011-02-10 23:10                   ` Greg Klanderman
2011-02-11  1:42                   ` Greg Klanderman
2011-02-11  3:29                     ` Bart Schaefer
2011-02-11 16:28                       ` Greg Klanderman
2011-02-11 17:26                         ` Bart Schaefer
2011-02-11 17:47                           ` Peter Stephenson
2011-02-11 18:36                           ` Bart Schaefer
2011-02-11 19:25                         ` Peter Stephenson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=110208205856.ZM24066@torch.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=zsh-workers@zsh.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).