Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm
Precedence: bulk
X-No-Archive: yes
List-Id: Zsh Workers List <zsh-workers.zsh.org>
List-Post: <mailto:zsh-workers@zsh.org>
List-Help: <mailto:zsh-workers-help@zsh.org>
X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on f.primenet.com.au
X-Spam-Level: 
X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM,
	T_DKIM_INVALID autolearn=ham autolearn_force=no version=3.4.0
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex.ru; s=mail;
	t=1454270651; bh=WEkXSRyfDiz9DXjMGc6aJmZynvr25bOUG/cSEPCo8cw=;
	h=From:To:Cc:In-Reply-To:References:Subject:Date;
	b=k/IlkLJiBDyrMdx2xkuaFyal9ZEVwhw3GtlYK/RWhl9FQHbQwYtp3YhqsxRWJU7Fz
	 OPjizw37BaGuTgq6GUm5mmo5qy8eryDA6OrJRgWTn0Uey5GI78m6S0MLj/ilslU72F
	 k1fExGgxPFR3OW5qKtQ6FGSu7JFLKtO5ZD4mEoH4=
From: "Nikolay Aleksandrovich Pavlov (ZyX)" <kp-pav@yandex.ru>
To: Sebastian Gniazdowski <sgniazdowski@gmail.com>
Cc: Zsh hackers list <zsh-workers@zsh.org>
In-Reply-To: <CAKc7PVCWzg0n352x9HSzLm9Q4CxBQVvruUb0YbYt2ERVbmCOwA@mail.gmail.com>
References: <CAKc7PVDMSiWckCwSijk8i6js86OCNeRuwfkVV1xjGMv_W554_Q@mail.gmail.com>
	 <7850171454263927@web30j.yandex.ru> <CAKc7PVCWzg0n352x9HSzLm9Q4CxBQVvruUb0YbYt2ERVbmCOwA@mail.gmail.com>
Subject: Re: emulate -L sh impact on $0, $argv
MIME-Version: 1.0
Message-Id: <2970481454270650@web26g.yandex.ru>
X-Mailer: Yamail [ http://yandex.ru ] 5.0
Date: Sun, 31 Jan 2016 23:04:10 +0300
Content-Transfer-Encoding: 8bit
Content-Type: text/plain; charset=koi8-r
X-Seq: zsh-workers 37853



31.01.2016, 22:14, "Sebastian Gniazdowski" <sgniazdowski@gmail.com>:
> On 31 January 2016 at 19:12, Nikolay Aleksandrovich Pavlov (ZyX)
> <kp-pav@yandex.ru> wrote:
>> šAnd in any case has nothing to do with $0, $0 is never present in $argv.
>
> Are you sure? The examples show $0 is in $argv[0]

The example shows `source` in $0. Script name in $argv[0] is *not* $0, it is `source` argument, you may see it in $1 as well.

Best way to examine `$argv` content is

    () { emulate -L zsh ; builtin source <(<<< 'printf "<%s>\n" $0 $argv') }

(note: temporary file is not needed, `builtin` is present because my `source` is a function which sets localoptions and noaliases): this will print something like

    </proc/self/fd/11>

: in place of printing any arguments it prints only $0.

If I remove `builtin` here, leaving my function

    source () {
            setopt localoptions
            setopt noaliases
            builtin source "${@[@]}"
    }

(somewhat similar to your function) it will show that when arguments are not empty $1 is the same as $0, and if there are some arguments $1 is first argument given. ***But it is $1.*** Not sure whether or not this is a bug:

    () { emulate -L zsh ; source =(<<< 'printf "<%s>\n" $0 $argv') }
    </tmp/zshENNMpE>
    </tmp/zshENNMpE>
    () { emulate -L zsh ; source =(<<< 'printf "<%s>\n" $0 $argv') a b }
    </tmp/zshTUoQ9c>
    <a>
    <b>

. You see: no $0 in $argv in second example (my function for some reason does not work with fds, so I used `=()` and not `<()`).

And note the documentation: it explicitly states that `$argv` is the same thing as `$*` and `$*` is an array containing positional parameters. Even if there *was* `$0` in `$argv` it would be a *bug*.

>
>> šArray indexing *may* start with zero, controlled by KSH_ARRAYS
>> šoption which is again different in zsh and sh emulation mode.
>
> In sh argv[0] is the same as argv[1]? Because again, you say I
> shouldn't be able to use argv[0] without KSH_ARRAYS, but in the
> examples, I do

In sh KSH_ARRAYS is set, so indexes are shifted.

>
> Best regards,
> Sebastian Gniazdowski

