zsh-users
 help / color / mirror / code / Atom feed
* set temporary environment variables for builtins
@ 2023-12-16 17:59 Clinton Bunch
  2023-12-16 18:53 ` Lawrence Velázquez
  0 siblings, 1 reply; 11+ messages in thread
From: Clinton Bunch @ 2023-12-16 17:59 UTC (permalink / raw)
  To: zsh-users

I was playing around with date formats using print -P and tried to get 
the time in Paris.

This didn't work:

TZ=Europe/Paris print -P '%D{%Y%m%dT%H%M%S%z}'

This does:

export TZ=Europe/Paris

print -P '%D{%Y%m%dT%H%M%S%z}'


but I have to unset/restore TZ afterwards.


I also tried:

() { typeset -x TZ=Europe/Paris; print -P '%D{%Y%m%dT%H%M%S%z}' }

It left me with TZ set.

I know this can be done with the date command, but it's one of those 
things that went from "Let's try this" to "Surely, there's a way to do this"

Is this a bug or design decision?

echo $ZSH_VERSION $ZSH_PATCHLEVEL $OSTYPE
5.8 zsh-5.8-0-g77d203f linux-gnu

cat /etc/system-release
Rocky Linux release 9.3 (Blue Onyx)



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

* Re: set temporary environment variables for builtins
  2023-12-16 17:59 set temporary environment variables for builtins Clinton Bunch
@ 2023-12-16 18:53 ` Lawrence Velázquez
  2023-12-16 19:48   ` Clinton Bunch
  0 siblings, 1 reply; 11+ messages in thread
From: Lawrence Velázquez @ 2023-12-16 18:53 UTC (permalink / raw)
  To: Clinton Bunch; +Cc: zsh-users

On Sat, Dec 16, 2023, at 12:59 PM, Clinton Bunch wrote:
> I also tried:
>
> () { typeset -x TZ=Europe/Paris; print -P '%D{%Y%m%dT%H%M%S%z}' }
>
> It left me with TZ set.

With GLOBAL_EXPORT enabled (which is the default), ''typeset -x''
acts like ''typeset -gx''.  Use ''local -x''.

	% typeset -p TZ
	typeset: no such variable: TZ
	% () { local -x TZ=Europe/Paris; print -P '%D{%z}' }
	+0100
	% typeset -p TZ
	typeset: no such variable: TZ

-- 
vq


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

* Re: set temporary environment variables for builtins
  2023-12-16 18:53 ` Lawrence Velázquez
@ 2023-12-16 19:48   ` Clinton Bunch
  2023-12-16 20:20     ` Bart Schaefer
  2023-12-16 20:21     ` Lawrence Velázquez
  0 siblings, 2 replies; 11+ messages in thread
From: Clinton Bunch @ 2023-12-16 19:48 UTC (permalink / raw)
  To: zsh-users

Thanks.  That worked.

Still wondering why the standard syntax for setting a temporary 
environment variable doesn't work for builtins.

I don't think I'm unusual in expecting this to work:

TZ=Europe/Paris print -P '%D{%z}'

Granted this is much more likely something someone would use with TZ=UTC.

On 12/16/2023 12:53 PM, Lawrence Velázquez wrote:
> On Sat, Dec 16, 2023, at 12:59 PM, Clinton Bunch wrote:
>> I also tried:
>>
>> () { typeset -x TZ=Europe/Paris; print -P '%D{%Y%m%dT%H%M%S%z}' }
>>
>> It left me with TZ set.
> With GLOBAL_EXPORT enabled (which is the default), ''typeset -x''
> acts like ''typeset -gx''.  Use ''local -x''.
>
> 	% typeset -p TZ
> 	typeset: no such variable: TZ
> 	% () { local -x TZ=Europe/Paris; print -P '%D{%z}' }
> 	+0100
> 	% typeset -p TZ
> 	typeset: no such variable: TZ
>


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

* Re: set temporary environment variables for builtins
  2023-12-16 19:48   ` Clinton Bunch
@ 2023-12-16 20:20     ` Bart Schaefer
  2023-12-16 20:21     ` Lawrence Velázquez
  1 sibling, 0 replies; 11+ messages in thread
From: Bart Schaefer @ 2023-12-16 20:20 UTC (permalink / raw)
  To: zsh-users

On Sat, Dec 16, 2023 at 11:48 AM Clinton Bunch <cdb_zsh@zentaur.org> wrote:
>
> Still wondering why the standard syntax for setting a temporary
> environment variable doesn't work for builtins.
>
> I don't think I'm unusual in expecting this to work:

https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09_01
(2.9.1 Simple Commands)

"If the command name is not a special built-in utility or function,
the variable assignments shall be exported for the execution
environment of the command and
  (my emphasis) shall not affect the current execution environment
except as a side-effect of the expansions performed in step 4"

"print" is not a "special" built-in, so "shall not affect" applies.  "Step 4" is

"Each variable assignment shall be expanded for tilde expansion,
parameter expansion, command substitution, arithmetic expansion, and
quote removal prior to assigning the value."


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

* Re: set temporary environment variables for builtins
  2023-12-16 19:48   ` Clinton Bunch
  2023-12-16 20:20     ` Bart Schaefer
@ 2023-12-16 20:21     ` Lawrence Velázquez
  2023-12-16 20:27       ` Bart Schaefer
  1 sibling, 1 reply; 11+ messages in thread
From: Lawrence Velázquez @ 2023-12-16 20:21 UTC (permalink / raw)
  To: Clinton Bunch; +Cc: zsh-users

On Sat, Dec 16, 2023, at 2:48 PM, Clinton Bunch wrote:
> Still wondering why the standard syntax for setting a temporary 
> environment variable doesn't work for builtins.

It does work, in general.

	% print -P '%D{%B}'
	December
	% LC_TIME=fr_FR.UTF-8 print -P '%D{%B}'
	décembre
	% print -r -- $LC_TIME

	%

> I don't think I'm unusual in expecting this to work:
>
> TZ=Europe/Paris print -P '%D{%z}'

The issue might be unique to TZ or TZ + print, but I'll defer to
someone who actually has a clue (or has time to unearth one).

-- 
vq


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

* Re: set temporary environment variables for builtins
  2023-12-16 20:21     ` Lawrence Velázquez
@ 2023-12-16 20:27       ` Bart Schaefer
  2023-12-16 20:31         ` Roman Perepelitsa
  2023-12-17  0:26         ` Lawrence Velázquez
  0 siblings, 2 replies; 11+ messages in thread
From: Bart Schaefer @ 2023-12-16 20:27 UTC (permalink / raw)
  To: Lawrence Velázquez; +Cc: Clinton Bunch, zsh-users

On Sat, Dec 16, 2023 at 12:22 PM Lawrence Velázquez <larryv@zsh.org> wrote:
>
> It does work, in general.

Well, no, it doesn't.  It works for parameters that zsh separately
considers to be "special", which includes all the LC_* variants.  TZ
is not special.


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

* Re: set temporary environment variables for builtins
  2023-12-16 20:27       ` Bart Schaefer
@ 2023-12-16 20:31         ` Roman Perepelitsa
  2023-12-16 20:35           ` Bart Schaefer
  2023-12-16 23:53           ` Mikael Magnusson
  2023-12-17  0:26         ` Lawrence Velázquez
  1 sibling, 2 replies; 11+ messages in thread
From: Roman Perepelitsa @ 2023-12-16 20:31 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Lawrence Velázquez, Clinton Bunch, zsh-users

On Sat, Dec 16, 2023 at 9:28 PM Bart Schaefer <schaefer@brasslantern.com> wrote:
>
> On Sat, Dec 16, 2023 at 12:22 PM Lawrence Velázquez <larryv@zsh.org> wrote:
> >
> > It does work, in general.
>
> Well, no, it doesn't.  It works for parameters that zsh separately
> considers to be "special", which includes all the LC_* variants.  TZ
> is not special.

In which sense is LC_TIME special for print but TZ is not?

Roman.


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

* Re: set temporary environment variables for builtins
  2023-12-16 20:31         ` Roman Perepelitsa
@ 2023-12-16 20:35           ` Bart Schaefer
  2023-12-16 23:53           ` Mikael Magnusson
  1 sibling, 0 replies; 11+ messages in thread
From: Bart Schaefer @ 2023-12-16 20:35 UTC (permalink / raw)
  To: Roman Perepelitsa; +Cc: Lawrence Velázquez, Clinton Bunch, zsh-users

On Sat, Dec 16, 2023 at 12:32 PM Roman Perepelitsa
<roman.perepelitsa@gmail.com> wrote:
>
> In which sense is LC_TIME special for print but TZ is not?

In the sense that LC_* are always special, as noted by the <S>
notation in "man zshparam".


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

* Re: set temporary environment variables for builtins
  2023-12-16 20:31         ` Roman Perepelitsa
  2023-12-16 20:35           ` Bart Schaefer
@ 2023-12-16 23:53           ` Mikael Magnusson
  1 sibling, 0 replies; 11+ messages in thread
From: Mikael Magnusson @ 2023-12-16 23:53 UTC (permalink / raw)
  To: Roman Perepelitsa; +Cc: zsh-users

On 12/16/23, Roman Perepelitsa <roman.perepelitsa@gmail.com> wrote:
> On Sat, Dec 16, 2023 at 9:28 PM Bart Schaefer <schaefer@brasslantern.com>
> wrote:
>>
>> On Sat, Dec 16, 2023 at 12:22 PM Lawrence Velázquez <larryv@zsh.org>
>> wrote:
>> >
>> > It does work, in general.
>>
>> Well, no, it doesn't.  It works for parameters that zsh separately
>> considers to be "special", which includes all the LC_* variants.  TZ
>> is not special.
>
> In which sense is LC_TIME special for print but TZ is not?

Setting any of the LC_* parameters explicitly calls some locale stuff
(whether or not it is exported), apart from just setting the
parameters:

/**/
void
lcsetfn(Param pm, char *x)
{
    char *x2;
    struct localename *ln;

    strsetfn(pm, x);
    if ((x2 = getsparam("LC_ALL")) && *x2)
	return;
    queue_signals();
    /* Treat empty LC_* the same as unset. */
    if (!x || !*x)
	x = getsparam("LANG");

    /*
     * If we've got no non-empty string at this
     * point (after checking $LANG, too),
     * we shouldn't bother setting anything.
     */
    if (x && *x) {
	for (ln = lc_names; ln->name; ln++)
	    if (!strcmp(ln->name, pm->node.nam))
		setlocale(ln->category, unmeta(x));
    }
    unqueue_signals();
    clear_mbstate();	/* LC_CTYPE may have changed */
    inittyptab();
}


-- 
Mikael Magnusson


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

* Re: set temporary environment variables for builtins
  2023-12-16 20:27       ` Bart Schaefer
  2023-12-16 20:31         ` Roman Perepelitsa
@ 2023-12-17  0:26         ` Lawrence Velázquez
  2023-12-17  7:54           ` Roman Perepelitsa
  1 sibling, 1 reply; 11+ messages in thread
From: Lawrence Velázquez @ 2023-12-17  0:26 UTC (permalink / raw)
  To: Bart Schaefer; +Cc: Clinton Bunch, zsh-users

On Sat, Dec 16, 2023, at 3:27 PM, Bart Schaefer wrote:
> On Sat, Dec 16, 2023 at 12:22 PM Lawrence Velázquez <larryv@zsh.org> wrote:
>>
>> It does work, in general.
>
> Well, no, it doesn't.  It works for parameters that zsh separately
> considers to be "special", which includes all the LC_* variants.  TZ
> is not special.

Huh, this is very surprising to me.  I guess I never noticed because
almost all the variables that can influence builtins (e.g., CDPATH,
LC_*, OPTIND, etc.) are special.

-- 
vq


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

* Re: set temporary environment variables for builtins
  2023-12-17  0:26         ` Lawrence Velázquez
@ 2023-12-17  7:54           ` Roman Perepelitsa
  0 siblings, 0 replies; 11+ messages in thread
From: Roman Perepelitsa @ 2023-12-17  7:54 UTC (permalink / raw)
  To: Lawrence Velázquez; +Cc: Bart Schaefer, Clinton Bunch, zsh-users

On Sun, Dec 17, 2023 at 1:27 AM Lawrence Velázquez <larryv@zsh.org> wrote:
>
> On Sat, Dec 16, 2023, at 3:27 PM, Bart Schaefer wrote:
> > On Sat, Dec 16, 2023 at 12:22 PM Lawrence Velázquez <larryv@zsh.org> wrote:
> >>
> >> It does work, in general.
> >
> > Well, no, it doesn't.  It works for parameters that zsh separately
> > considers to be "special", which includes all the LC_* variants.  TZ
> > is not special.
>
> Huh, this is very surprising to me.  I guess I never noticed because
> almost all the variables that can influence builtins (e.g., CDPATH,
> LC_*, OPTIND, etc.) are special.

I am also surprised by this. It does make sense given the
implementation details but it's not how I expected zsh to behave.

Would it make sense to make all parameters that affect builtins
special? Are there parameters other than TZ that affect builtins but
aren't already special?

Roman.


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

end of thread, other threads:[~2023-12-17 11:36 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-16 17:59 set temporary environment variables for builtins Clinton Bunch
2023-12-16 18:53 ` Lawrence Velázquez
2023-12-16 19:48   ` Clinton Bunch
2023-12-16 20:20     ` Bart Schaefer
2023-12-16 20:21     ` Lawrence Velázquez
2023-12-16 20:27       ` Bart Schaefer
2023-12-16 20:31         ` Roman Perepelitsa
2023-12-16 20:35           ` Bart Schaefer
2023-12-16 23:53           ` Mikael Magnusson
2023-12-17  0:26         ` Lawrence Velázquez
2023-12-17  7:54           ` Roman Perepelitsa

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