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

Re: how to refer to basename of $0



On 2011-07-28 at 18:55 -0400, TJ Luoma wrote:
> the script "foo.sh" read .source like this:
> 
> 	. $HOME/.source
> 
> and then I did
> 
> 	echo "$NAME"
> 
> it would give me
> 
> 	foo.sh
> 
> but in zsh I get
> 
> 	zsh

Are you sure?

% cat -v foo
. $HOME/bar
% cat -v bar
echo $0
% zsh -f foo
/home/me/bar
% bash foo
foo

The point is that in bash, sourcing a script does not change $0 while in
zsh it does by default, because FUNCTION_ARGZERO is set.

% cat -v foo2
unsetopt function_argzero
. $HOME/bar
% zsh -f foo2
foo2

If you want to be portable to both bash and zsh, then:

  [[ -n $ZSH_VERSION ]] && unsetopt function_argzero

This does, unfortunately, have to be done in the script which does the
including, so you can't have a common library used by both shells which
assumes that $0 is the name of the original file and which can just be
simply included.

-Phil



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