zsh-workers
 help / color / mirror / Atom feed
* Question on unintuitive behaviour for function execution and parameter assignment
@ 2021-10-12  8:20 Jett Husher
  2021-10-12  8:31 ` Peter Stephenson
  0 siblings, 1 reply; 4+ messages in thread
From: Jett Husher @ 2021-10-12  8:20 UTC (permalink / raw)
  To: zsh-workers

Good day!

Does a function `assign-hello` treat parameter assignment on simple command as local parameter in the following code snipped?

```
HELLO=WORLD

function assign-hello() HELLO=$1

assign-hello THERE
echo $HELLO # Prints 'THERE', as expected

# This part trips me up
HELLO= assign-hello WHY
echo $HELLO # Why does it still print 'THERE'?
```

POSIX spec says that parameter assignment with function execution will alter the current shell environment and that it's not specified what's going to happen with this parameter at the end of the execution. `zshmisc(1)` in FUNCTIONS states that functions are called on a parent process, so my intuition was that this sort of code should not work for them. Unfortunately, I could not find any more information about this in the manuals or FAQs.

Can manuals be updated to cover this behaviour, please?

Thank you for your time.
- Jett


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

* Re: Question on unintuitive behaviour for function execution and parameter assignment
  2021-10-12  8:20 Question on unintuitive behaviour for function execution and parameter assignment Jett Husher
@ 2021-10-12  8:31 ` Peter Stephenson
  2021-10-13  8:42   ` Jett Husher
  0 siblings, 1 reply; 4+ messages in thread
From: Peter Stephenson @ 2021-10-12  8:31 UTC (permalink / raw)
  To: Jett Husher, zsh-workers


> On 12 October 2021 at 09:20 Jett Husher <jetthusher@pm.me> wrote:
> 
> 
> Good day!
> 
> Does a function `assign-hello` treat parameter assignment on simple command as local parameter in the following code snipped?
> 
> ```
> HELLO=WORLD
> 
> function assign-hello() HELLO=$1
> 
> assign-hello THERE
> echo $HELLO # Prints 'THERE', as expected
> 
> # This part trips me up
> HELLO= assign-hello WHY
> echo $HELLO # Why does it still print 'THERE'?
> ```

This is covered by the POSIX_BUILTINS option.  Generally, the POSIX_* options are
the place to look for this sort of think --- granted that can be a bit of hunt.

Usually an easy test to see if zsh does have the POSIX behaviour available is
to start a new shell as

ARGV0=sh zsh

and see what behaviour that gives you.  That should be maximally compatible,
although it doesn't help you find which option controls  the behaviour.

POSIX_BUILTINS <K> <S>
    When this option is set the command builtin can be used  to  execute  shell
    builtin  commands.   Parameter assignments specified before shell functions
    and special builtins are kept after the command completes unless  the  spe‐
    cial builtin is prefixed with the command builtin.  Special builtins are .,
    :, break, continue, declare, eval, exit, export, integer, local,  readonly,
    return, set, shift, source, times, trap and unset.

pws


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

* Re: Question on unintuitive behaviour for function execution and parameter assignment
  2021-10-12  8:31 ` Peter Stephenson
@ 2021-10-13  8:42   ` Jett Husher
  2021-10-13  9:10     ` Peter Stephenson
  0 siblings, 1 reply; 4+ messages in thread
From: Jett Husher @ 2021-10-13  8:42 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: zsh-workers

On Tuesday, October 12th, 2021 at 10:31, Peter Stephenson <p.w.stephenson@ntlworld.com> wrote:
> This is covered by the POSIX_BUILTINS option. Generally, the POSIX_* options are
>
> the place to look for this sort of think --- granted that can be a bit of hunt.

I wouldn't even think to look at POSIX_* variables because POSIX-2017
explicitly tells that it's unspecified "Whether or not the variable assignments persist after
the completion of the function", so, naturally, I though that this behaviour was compliant.

> Usually an easy test to see if zsh does have the POSIX behaviour available is
>
> to start a new shell as
>
> ARGV0=sh zsh
>
> and see what behaviour that gives you. That should be maximally compatible,
>
> although it doesn't help you find which option controls the behaviour.

I would keep that in mind next time. I used to compare the executions in bash, since I thought
it's more compliant, but I guess it's not the best option since GNU also states that it
takes the standards as a recommendation and not a rule.

I still believe this needs to be mentioned in FUNCTIONS or SIMPLE COMMANDS & PIPELINES of zshmisc(1).
Or zshparam(1) perhaps? I would love to suggest my help with updating man pages but I'm not sure
if you accept commits the side. Or even how the development is done around here :shrug:

Either way, a world of thanks for the help, Peter.
- Jett


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

* Re: Question on unintuitive behaviour for function execution and parameter assignment
  2021-10-13  8:42   ` Jett Husher
@ 2021-10-13  9:10     ` Peter Stephenson
  0 siblings, 0 replies; 4+ messages in thread
From: Peter Stephenson @ 2021-10-13  9:10 UTC (permalink / raw)
  To: zsh-workers


> On 13 October 2021 at 09:42 Jett Husher <jetthusher@pm.me> wrote:
> On Tuesday, October 12th, 2021 at 10:31, Peter Stephenson <p.w.stephenson@ntlworld.com> wrote:
> > This is covered by the POSIX_BUILTINS option. Generally, the POSIX_* options are
> >
> > the place to look for this sort of think --- granted that can be a bit of hunt.
> 
> I wouldn't even think to look at POSIX_* variables because POSIX-2017
> explicitly tells that it's unspecified "Whether or not the variable assignments persist after
> the completion of the function", so, naturally, I though that this behaviour was compliant.

It's always worth (everyone) bearing in mind that zsh is not a POSIX shell by default.

In fact, it's not *completely* POSIX even in sh emulation, as this has been gradually
retrofitted, and not everything can be done within the current constraints.

cheers
pws


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

end of thread, other threads:[~2021-10-13  9:10 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-10-12  8:20 Question on unintuitive behaviour for function execution and parameter assignment Jett Husher
2021-10-12  8:31 ` Peter Stephenson
2021-10-13  8:42   ` Jett Husher
2021-10-13  9:10     ` Peter Stephenson

zsh-workers

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://inbox.vuxu.org/zsh-workers

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V1 zsh-workers zsh-workers/ https://inbox.vuxu.org/zsh-workers \
		zsh-workers@zsh.org
	public-inbox-index zsh-workers

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.workers


code repositories for the project(s) associated with this inbox:

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

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git