zsh-workers
 help / color / mirror / code / Atom feed
* emulate -L sh impact on $0, $argv
@ 2016-01-31 18:03 Sebastian Gniazdowski
  2016-01-31 18:12 ` Nikolay Aleksandrovich Pavlov (ZyX)
  0 siblings, 1 reply; 7+ messages in thread
From: Sebastian Gniazdowski @ 2016-01-31 18:03 UTC (permalink / raw)
  To: Zsh hackers list

Hello
Is this expected behavior:

% echo 'echo Hello, my 0 is $0, argv0 is ${argv[0]}' > test_file.sh;
source() { emulate -L zsh; builtin source "$@"; }; source
./test_file.sh
Hello, my 0 is ./test_file.sh, argv0 is

% echo 'echo Hello, my 0 is $0, argv0 is ${argv[0]}' > test_file.sh;
source() { emulate -L sh; builtin source "$@"; }; source
./test_file.sh
Hello, my 0 is source, argv0 is ./test_file.sh

Sometimes -L zsh gives correct argv[0], couldn't clarify exactly when,
it may have something to do with reversed order of the calls.

Best regards,
Sebastian Gniazdowski


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: emulate -L sh impact on $0, $argv
  2016-01-31 18:03 emulate -L sh impact on $0, $argv Sebastian Gniazdowski
@ 2016-01-31 18:12 ` Nikolay Aleksandrovich Pavlov (ZyX)
  2016-01-31 18:43   ` Bart Schaefer
  2016-01-31 19:13   ` Sebastian Gniazdowski
  0 siblings, 2 replies; 7+ messages in thread
From: Nikolay Aleksandrovich Pavlov (ZyX) @ 2016-01-31 18:12 UTC (permalink / raw)
  To: Sebastian Gniazdowski, Zsh hackers list



31.01.2016, 21:04, "Sebastian Gniazdowski" <sgniazdowski@gmail.com>:
> Hello
> Is this expected behavior:
>
> % echo 'echo Hello, my 0 is $0, argv0 is ${argv[0]}' > test_file.sh;
> source() { emulate -L zsh; builtin source "$@"; }; source
> ./test_file.sh
> Hello, my 0 is ./test_file.sh, argv0 is
>
> % echo 'echo Hello, my 0 is $0, argv0 is ${argv[0]}' > test_file.sh;
> source() { emulate -L sh; builtin source "$@"; }; source
> ./test_file.sh
> Hello, my 0 is source, argv0 is ./test_file.sh
>
> Sometimes -L zsh gives correct argv[0], couldn't clarify exactly when,
> it may have something to do with reversed order of the calls.

Array indexing starts with one, not zero, so $argv[0] in zsh emulation mode is not what you think: it is empty. *Always*. And in any case has nothing to do with $0, $0 is never present in $argv. Array indexing *may* start with zero, controlled by KSH_ARRAYS option which is again different in zsh and sh emulation mode.

What is contained is $0 is controlled by FUNCTION_ARGZERO option, which is different in zsh and sh modes.

>
> Best regards,
> Sebastian Gniazdowski


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: emulate -L sh impact on $0, $argv
  2016-01-31 18:12 ` Nikolay Aleksandrovich Pavlov (ZyX)
@ 2016-01-31 18:43   ` Bart Schaefer
  2016-01-31 19:13   ` Sebastian Gniazdowski
  1 sibling, 0 replies; 7+ messages in thread
From: Bart Schaefer @ 2016-01-31 18:43 UTC (permalink / raw)
  To: Zsh hackers list

On Jan 31,  9:12pm, Nikolay Aleksandrovich Pavlov (ZyX) wrote:
}
} What is contained is $0 is controlled by FUNCTION_ARGZERO option,
} which is different in zsh and sh modes.

In recent zsh this is also affected by the POSIX_ARGZERO option,
but that is off by default in all emulations because of backwards-
compatibility concerns.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: emulate -L sh impact on $0, $argv
  2016-01-31 18:12 ` Nikolay Aleksandrovich Pavlov (ZyX)
  2016-01-31 18:43   ` Bart Schaefer
@ 2016-01-31 19:13   ` Sebastian Gniazdowski
  2016-01-31 20:04     ` Nikolay Aleksandrovich Pavlov (ZyX)
  2016-01-31 20:37     ` Bart Schaefer
  1 sibling, 2 replies; 7+ messages in thread
From: Sebastian Gniazdowski @ 2016-01-31 19:13 UTC (permalink / raw)
  To: Nikolay Aleksandrovich Pavlov (ZyX); +Cc: Zsh hackers list

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]

> 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

Best regards,
Sebastian Gniazdowski


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: emulate -L sh impact on $0, $argv
  2016-01-31 19:13   ` Sebastian Gniazdowski
@ 2016-01-31 20:04     ` Nikolay Aleksandrovich Pavlov (ZyX)
  2016-01-31 20:37     ` Bart Schaefer
  1 sibling, 0 replies; 7+ messages in thread
From: Nikolay Aleksandrovich Pavlov (ZyX) @ 2016-01-31 20:04 UTC (permalink / raw)
  To: Sebastian Gniazdowski; +Cc: Zsh hackers list



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


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: emulate -L sh impact on $0, $argv
  2016-01-31 19:13   ` Sebastian Gniazdowski
  2016-01-31 20:04     ` Nikolay Aleksandrovich Pavlov (ZyX)
@ 2016-01-31 20:37     ` Bart Schaefer
  2016-02-07  0:22       ` Daniel Shahaf
  1 sibling, 1 reply; 7+ messages in thread
From: Bart Schaefer @ 2016-01-31 20:37 UTC (permalink / raw)
  To: Zsh hackers list

On Jan 31,  8:13pm, Sebastian Gniazdowski wrote:
}
} 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

This is a side-effect KSH_ARRAYS combined with the behavior of
$argv / $* / $@ when in the "source" command.  Doc of "." command:

     If any arguments ARG are given, they become the positional
     parameters; the old positional parameters are restored when the
     FILE is done executing.

What's left unsaid there is that if NO arguments are given, then the
positional parameters REMAIN THOSE OF THE CALLING CONTEXT.  So argv[0]
in your example is not test_file.sh's $0, it's the "source" FUNCTION's
$argv[1].

If you change your example to:

echo 'echo 0 is $0, argv0 is ${argv[0]}, argv1 is ${argv[1]}' > test_file.sh;
source ./test_file.sh ARGUMENT

Then the zsh case

    source() { emulate -L zsh; builtin source "$@"; }

will give

    0 is ./test_file.sh, argv0 is , argv1 is ARGUMENT

whereas the sh case

    source() { emulate -L sh; builtin source "$@"; }

yields

    0 is source, argv0 is ARGUMENT, argv1 is 

With KSH_ARRAYS set, $argv[0] == $1, $argv[1] == $2, etc.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: emulate -L sh impact on $0, $argv
  2016-01-31 20:37     ` Bart Schaefer
@ 2016-02-07  0:22       ` Daniel Shahaf
  0 siblings, 0 replies; 7+ messages in thread
From: Daniel Shahaf @ 2016-02-07  0:22 UTC (permalink / raw)
  To: Zsh hackers list

Bart Schaefer wrote on Sun, Jan 31, 2016 at 12:37:45 -0800:
> On Jan 31,  8:13pm, Sebastian Gniazdowski wrote:
> }
> } 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
> 
> This is a side-effect KSH_ARRAYS combined with the behavior of
> $argv / $* / $@ when in the "source" command.  Doc of "." command:
> 
>      If any arguments ARG are given, they become the positional
>      parameters; the old positional parameters are restored when the
>      FILE is done executing.
> 
> What's left unsaid there is that if NO arguments are given, then the
> positional parameters REMAIN THOSE OF THE CALLING CONTEXT.  So argv[0]
> in your example is not test_file.sh's $0, it's the "source" FUNCTION's
> $argv[1].

diff --git a/Doc/Zsh/builtins.yo b/Doc/Zsh/builtins.yo
index 542cecf..5aebdef 100644
--- a/Doc/Zsh/builtins.yo
+++ b/Doc/Zsh/builtins.yo
@@ -81,6 +81,10 @@ then commands are read from that file instead of var(file).
 If any arguments var(arg) are given,
 they become the positional parameters; the old positional
 parameters are restored when the var(file) is done executing.
+However, if no arguments are given,
+the positional parameters remain those of the calling context,
+and no restoring is done.
+
 If var(file) was not found the return status is 127; if var(file) was found
 but contained a syntax error the return status is 126; else the return
 status is the exit status of the last command executed.


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2016-02-07  0:28 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-01-31 18:03 emulate -L sh impact on $0, $argv Sebastian Gniazdowski
2016-01-31 18:12 ` Nikolay Aleksandrovich Pavlov (ZyX)
2016-01-31 18:43   ` Bart Schaefer
2016-01-31 19:13   ` Sebastian Gniazdowski
2016-01-31 20:04     ` Nikolay Aleksandrovich Pavlov (ZyX)
2016-01-31 20:37     ` Bart Schaefer
2016-02-07  0:22       ` Daniel Shahaf

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).