zsh-workers
 help / color / mirror / Atom feed
* [bug] Special parameters $PATH and $path aren't kept in sync after emulating sh
@ 2020-11-09  7:27 Brice Waegeneire
  2020-11-09 10:07 ` Peter Stephenson
  2020-11-09 10:22 ` Daniel Shahaf
  0 siblings, 2 replies; 3+ messages in thread
From: Brice Waegeneire @ 2020-11-09  7:27 UTC (permalink / raw)
  To: zsh-workers

Hello,

Yesterday I stumbled upon a bug where setting $PATH when emulating
sh would would break the variable synchronization between the string
($PATH) and the array ($path):
--8<---------------cut here---------------start------------->8---
$ echo $PATH
/gnu/store/j1jv0f6cs18s76jbkp8h7nbh1i7rwgz2-profile/bin:/home/bricewge/.zsh/polaris/sbin:/home/bricewge/.zsh/polaris/bin:/home/bricewge/.zsh/polaris/usr/sbin:/home/bricewge/.zsh/polaris/usr/bin:/home/bricewge/misc/go/bin:/home/bricewge/.emacs.d/bin:/home/bricewge/.local/bin:/run/setuid-programs:/home/bricewge/.config/guix/current/bin:/home/bricewge/.guix-profile/bin:/home/bricewge/.guix-profile/sbin:/run/current-system/profile/bin:/run/current-system/profile/sbin
$ echo $path
/gnu/store/j1jv0f6cs18s76jbkp8h7nbh1i7rwgz2-profile/bin 
/home/bricewge/.zsh/polaris/sbin /home/bricewge/.zsh/polaris/bin 
/home/bricewge/.zsh/polaris/usr/sbin /home/bricewge/.zsh/polaris/usr/bin 
/home/bricewge/misc/go/bin /home/bricewge/.emacs.d/bin 
/home/bricewge/.local/bin /run/setuid-programs 
/home/bricewge/.config/guix/current/bin /home/bricewge/.guix-profile/bin 
/home/bricewge/.guix-profile/sbin /run/current-system/profile/bin 
/run/current-system/profile/sbin
$ cat /tmp/test.sh
unset PATH
export PATH=/foo
$ emulate sh -c 'source /tmp/test.sh'
_direnv_hook:1: command not found: direnv
$ echo $PATH
/foo
$ echo $path

$ export PATH=bar:baz
$ echo $PATH
bar:baz
$ echo $path
--8<---------------cut here---------------end--------------->8---

In effect it overwride my path instead of adding a new entry to it
when I do it from the array variable ($path) after setting it from
from the string variable ($PATH). To workaround this issue I have
found the following to work:

--8<---------------cut here---------------start------------->8---
$ tmp="$PATH"
$ unset PATH
$ export PATH="$tmp"
$ unset tmp
--8<---------------cut here---------------end--------------->8---

Cheers,
- Brice


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

* Re: [bug] Special parameters $PATH and $path aren't kept in sync after emulating sh
  2020-11-09  7:27 [bug] Special parameters $PATH and $path aren't kept in sync after emulating sh Brice Waegeneire
@ 2020-11-09 10:07 ` Peter Stephenson
  2020-11-09 10:22 ` Daniel Shahaf
  1 sibling, 0 replies; 3+ messages in thread
From: Peter Stephenson @ 2020-11-09 10:07 UTC (permalink / raw)
  To: zsh-workers

> On 09 November 2020 at 07:27 Brice Waegeneire <brice@waegenei.re> wrote:
> Yesterday I stumbled upon a bug where setting $PATH when emulating
> sh would would break the variable synchronization between the string
> ($PATH) and the array ($path):

sh mode doesn't have the special variable path; you need to rely exclusively
on PATH within sh emulation.  The basic idea of sh emulation is to track
POSIX as closely as possible to make sure all the basics work --- it's
not designed as an all-singing-all-dancing smart shell in the same
way normal zsh mode is.  In this case, that means we try to make sure variables
people might use unsuspectingly in sh don't get overloaded with special
behaviour.  (Having said that, zsh is written in such a way there are various
places where we can't make sh emulation work properly, but that's going rather
beyond your issue.)

Hooking up normal zsh behaviour and sh behaviour together can be a bit
of a nuisance exactly because, by design, they work differently.  So if
you've got a workaround, I'm afraid that may be as good as it gets.

pws


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

* Re: [bug] Special parameters $PATH and $path aren't kept in sync after emulating sh
  2020-11-09  7:27 [bug] Special parameters $PATH and $path aren't kept in sync after emulating sh Brice Waegeneire
  2020-11-09 10:07 ` Peter Stephenson
@ 2020-11-09 10:22 ` Daniel Shahaf
  1 sibling, 0 replies; 3+ messages in thread
From: Daniel Shahaf @ 2020-11-09 10:22 UTC (permalink / raw)
  To: Brice Waegeneire, zsh-workers

Brice Waegeneire wrote on Mon, 09 Nov 2020 07:27 +00:00:
> $ cat /tmp/test.sh
> unset PATH
> export PATH=/foo
> $ emulate sh -c 'source /tmp/test.sh'
> _direnv_hook:1: command not found: direnv
> $ echo $PATH
> /foo
> $ echo $path
> 
> $ export PATH=bar:baz
> $ echo $PATH
> bar:baz
> $ echo $path
> --8<---------------cut here---------------end--------------->8---
> 
> In effect it overwride my path instead of adding a new entry to it

Your code is not expected to append an entry to $PATH.  To do that,
you'd want to do «PATH="${PATH}:/new/entry"» (plus or minus the «export»
keyword in front).

> when I do it from the array variable ($path) after setting it from
> from the string variable ($PATH). To workaround this issue I have
> found the following to work:
> 
> --8<---------------cut here---------------start------------->8---
> $ tmp="$PATH"
> $ unset PATH
> $ export PATH="$tmp"
> $ unset tmp
> --8<---------------cut here---------------end--------------->8---

This doesn't append either.  (Also, a signal might come in whilst PATH
is undefined in the environment.)


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

end of thread, other threads:[~2020-11-09 10:23 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-09  7:27 [bug] Special parameters $PATH and $path aren't kept in sync after emulating sh Brice Waegeneire
2020-11-09 10:07 ` Peter Stephenson
2020-11-09 10:22 ` Daniel Shahaf

zsh-workers

This inbox may be cloned and mirrored by anyone:

	git clone --mirror http://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/ http://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