zsh-users
 help / color / mirror / code / Atom feed
* Temporary variable compatibility
@ 2023-03-22  0:21 Felipe Contreras
  2023-03-22  1:03 ` Lawrence Velázquez
  0 siblings, 1 reply; 2+ messages in thread
From: Felipe Contreras @ 2023-03-22  0:21 UTC (permalink / raw)
  To: Zsh Users

Hi,

I found a discrepancy between different shells:

  f() { echo $var; }
  var=1 f
  test -z "${var-}"

Both zsh, bash, and dash set the variable temporarily, but ksh does not.

In zsh the emulation of ksh and sh both keep the variable assignment.

Is this the POSIX standard? If so, then how should one temporarily set
a variable? A subshell?

Cheers.

-- 
Felipe Contreras


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

* Re: Temporary variable compatibility
  2023-03-22  0:21 Temporary variable compatibility Felipe Contreras
@ 2023-03-22  1:03 ` Lawrence Velázquez
  0 siblings, 0 replies; 2+ messages in thread
From: Lawrence Velázquez @ 2023-03-22  1:03 UTC (permalink / raw)
  To: Felipe Contreras; +Cc: zsh-users

On Tue, Mar 21, 2023, at 8:21 PM, Felipe Contreras wrote:
> I found a discrepancy between different shells:
>
>   f() { echo $var; }
>   var=1 f
>   test -z "${var-}"
>
> Both zsh, bash, and dash set the variable temporarily, but ksh does not.
>
> In zsh the emulation of ksh and sh both keep the variable assignment.
>
> Is this the POSIX standard?

POSIX.1-2017 leaves it unspecified, so both behaviors are conformant.
As per section 2.9.1 "Simple Commands" (e.g., something like "var=val
cmd"):

	- If the command name is a function that is not a standard
	  utility implemented as a function, variable assignments
	  shall affect the current execution environment during the
	  execution of the function.  It is unspecified:

		- Whether or not the variable assignments persist
		  after the completion of the function
		- Whether or not the variables gain the export
		  attribute during the execution of the function
		- Whether or not export attributes gained as a
		  result of the variable assignments persist after
		  the completion of the function (if variable
		  assignments persist after the completion of the
		  function)

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_01


> If so, then how should one temporarily set a variable? A subshell?

That's probably the most portable approach, yes.


-- 
vq


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

end of thread, other threads:[~2023-03-22  1:06 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-03-22  0:21 Temporary variable compatibility Felipe Contreras
2023-03-22  1:03 ` Lawrence Velázquez

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