Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Computer programs expand so as to fill the core available.


devel / comp.unix.shell / Bash ${!word} computed variable syntax.

SubjectAuthor
* Bash ${!word} computed variable syntax.Kaz Kylheku
+- Re: Bash ${!word} computed variable syntax.David W. Hodgins
+- Re: Bash ${!word} computed variable syntax.Kenny McCormack
+* Re: Bash ${!word} computed variable syntax.Javier
|+* Re: Bash ${!word} computed variable syntax.Christian Weisgerber
||`- Re: Bash ${!word} computed variable syntax.Kaz Kylheku
|+* Re: Bash ${!word} computed variable syntax.Kaz Kylheku
||+* Re: Bash ${!word} computed variable syntax.Janis Papanagnou
|||+- Re: Bash ${!word} computed variable syntax.Kaz Kylheku
|||+* Re: Bash ${!word} computed variable syntax.Javier
||||`* Re: Bash ${!word} computed variable syntax.Janis Papanagnou
|||| `- Re: Bash ${!word} computed variable syntax.Javier
|||`* Re: Bash ${!word} computed variable syntax.Javier
||| `- Re: Bash ${!word} computed variable syntax.Kaz Kylheku
||+* Re: Bash ${!word} computed variable syntax.Javier
|||`* Re: Bash ${!word} computed variable syntax.Kaz Kylheku
||| +* Re: Bash ${!word} computed variable syntax.Janis Papanagnou
||| |`- Re: Bash ${!word} computed variable syntax.Kaz Kylheku
||| `- Re: Bash ${!word} computed variable syntax.Javier
||`- Re: Bash ${!word} computed variable syntax.Keith Thompson
|`* ksh indirection [was: Bash ${!word} computed variable syntax]Martijn Dekker
| +* Re: ksh indirection [was: Bash ${!word} computed variable syntax]Kaz Kylheku
| |`- Re: ksh indirection [was: Bash ${!word} computed variable syntax]Janis Papanagnou
| `- Re: ksh indirection [was: Bash ${!word} computed variable syntax]Javier
`* Re: Bash ${!word} computed variable syntax.Keith Thompson
 `* Re: Bash ${!word} computed variable syntax.Kaz Kylheku
  `- Re: Bash ${!word} computed variable syntax.Javier

Pages:12
Bash ${!word} computed variable syntax.

<20230605202611.371@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=6322&group=comp.unix.shell#6322

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 864-117-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.shell
Subject: Bash ${!word} computed variable syntax.
Date: Tue, 6 Jun 2023 03:32:44 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 28
Message-ID: <20230605202611.371@kylheku.com>
Injection-Date: Tue, 6 Jun 2023 03:32:44 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="41dbf65b809ee029bb5c8293ea16e6c2";
logging-data="678838"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19c3dJQ4vLOZfZx+IbdYBmZdCd5cGQwtGg="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:OlkEDYDSc5xgZ6enX6pKnBzQvqQ=
 by: Kaz Kylheku - Tue, 6 Jun 2023 03:32 UTC

Curious situation.

Bash has a computed variable syntax. If there is a $word variable which
holds foo, then ${!word} will produce expansion $foo.

The only mention of this syntax is in an Appendix in the GNU Info
documentation which lists differences between Bash and The Bourne Shell.

There a single bullet point is made:

] * Bash has indirect variable expansion using ${!word} (see Shell
] Parameter Expansion).

But there is no mention of it in Shell Parameter Expansion.

The other {! gadgets are documented like ${!name[@]} and ${!pattern@}
and whatnot; but not that one.

The man page mentions nothing about it at all.

More curiously, this doc situation already exists in the oldest commit
in Bash git, which was an import of bash-2.0 sources dated 1996.
(I'm presuming that the feature worked then also.)

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: Bash ${!word} computed variable syntax.

<op.153nlfj1a3w0dxdave@hodgins.homeip.net>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=6323&group=comp.unix.shell#6323

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: dwhodg...@nomail.afraid.org (David W. Hodgins)
Newsgroups: comp.unix.shell
Subject: Re: Bash ${!word} computed variable syntax.
Date: Tue, 06 Jun 2023 00:18:41 -0400
Organization: A noiseless patient Spider
Lines: 36
Message-ID: <op.153nlfj1a3w0dxdave@hodgins.homeip.net>
References: <20230605202611.371@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed; delsp=yes
Content-Transfer-Encoding: 8bit
Injection-Info: dont-email.me; posting-host="64c1d1a52e480c10ebf53536b596cdb1";
logging-data="689139"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/ZobVYf2yEDU6V4cSwd/PtCdfSHkcWDH0="
User-Agent: Opera Mail/12.16 (Linux)
Cancel-Lock: sha1:wrXM8QxEPY7YxP2VPYTy3wKaiug=
 by: David W. Hodgins - Tue, 6 Jun 2023 04:18 UTC

On Mon, 05 Jun 2023 23:32:44 -0400, Kaz Kylheku <864-117-4973@kylheku.com> wrote:

> Curious situation.
>
> Bash has a computed variable syntax. If there is a $word variable which
> holds foo, then ${!word} will produce expansion $foo.
>
> The only mention of this syntax is in an Appendix in the GNU Info
> documentation which lists differences between Bash and The Bourne Shell.
>
> There a single bullet point is made:
>
> ] * Bash has indirect variable expansion using ${!word} (see Shell
> ] Parameter Expansion).
>
> But there is no mention of it in Shell Parameter Expansion.
>
> The other {! gadgets are documented like ${!name[@]} and ${!pattern@}
> and whatnot; but not that one.
>
> The man page mentions nothing about it at all.
>
> More curiously, this doc situation already exists in the oldest commit
> in Bash git, which was an import of bash-2.0 sources dated 1996.
> (I'm presuming that the feature worked then also.)

From the section Parameter Expansion of man bash ...
If the first character of parameter is an exclamation point (!), and parameter is not a nameref, it introduces a level of indirection. Bash uses the value formed by expanding the rest of parameter
as the new parameter; this is then expanded and that value is used in the rest of the expansion, rather than the expansion of the original parameter. This is known as indirect expansion. The
value is subject to tilde expansion, parameter expansion, command substitution, and arithmetic expansion. If parameter is a nameref, this expands to the name of the parameter referenced by parameā€
ter instead of performing the complete indirect expansion. The exceptions to this are the expansions of ${!prefix*} and ${!name[@]} described below. The exclamation point must immediately follow
the left brace in order to introduce indirection.

That's with GNU bash, version 5.1.16(1)-release

Regards, Dave Hodgins

Re: Bash ${!word} computed variable syntax.

<u5mcdi$k5t7$1@news.xmission.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=6324&group=comp.unix.shell#6324

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gaze...@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.unix.shell
Subject: Re: Bash ${!word} computed variable syntax.
Date: Tue, 6 Jun 2023 04:24:19 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <u5mcdi$k5t7$1@news.xmission.com>
References: <20230605202611.371@kylheku.com>
Injection-Date: Tue, 6 Jun 2023 04:24:19 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="661415"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Tue, 6 Jun 2023 04:24 UTC

In article <20230605202611.371@kylheku.com>,
Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>Curious situation.
>
>Bash has a computed variable syntax. If there is a $word variable which
>holds foo, then ${!word} will produce expansion $foo.
>
>The only mention of this syntax is in an Appendix in the GNU Info
>documentation which lists differences between Bash and The Bourne Shell.

(Assuming the point of your post is: Can you tell me where this is
documented?)

I found (in "man bash"):

${parameter}
The value of parameter is substituted. The braces are required
when parameter is a positional parameter with more than one
digit, or when parameter is followed by a character which is not
to be interpreted as part of its name. The parameter is a shell
parameter as described above PARAMETERS) or an array reference
(Arrays).

If the first character of parameter is an exclamation point (!), it
introduces a level of variable indirection. Bash uses the value of the
variable formed from the rest of parameter as the name of the variable;
this variable is then expanded and that value is used in the rest of the
substitution, rather than the value of parameter itself. This is known
as indirect expansion. The exceptions to this are the expansions of
${!prefix*} and ${!name[@]} described below. The exclamation point must
immediately follow the left brace in order to introduce indirection.

--
A racist, a Nazi, and a Klansman walk into a bar...

Bartender says, "What will it be, Mr. Trump?"

Re: Bash ${!word} computed variable syntax.

<D_6cnVNjRel7Ux35nZ2dnZfqnPudnZ2d@brightview.co.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=6331&group=comp.unix.shell#6331

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border-2.nntp.ord.giganews.com!nntp.giganews.com!Xl.tags.giganews.com!local-1.nntp.ord.giganews.com!nntp.brightview.co.uk!news.brightview.co.uk.POSTED!not-for-mail
NNTP-Posting-Date: Wed, 07 Jun 2023 18:39:34 +0000
From: inva...@invalid.invalid (Javier)
Subject: Re: Bash ${!word} computed variable syntax.
Newsgroups: comp.unix.shell
References: <20230605202611.371@kylheku.com>
Message-ID: <D_6cnVNjRel7Ux35nZ2dnZfqnPudnZ2d@brightview.co.uk>
Date: Wed, 07 Jun 2023 18:39:34 +0000
Lines: 17
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-cbBfj2i9R/s0d06aTezdOJTPqX+8LamghK5iZaUdz79mV2c1estjiXJHxL6khGoKU+NT8GHFCCFVG8a!sRMbXcWs6yzhhGvMDGPD8jyTYw3docPwIXAFL3ILTPKGK72J3MYzybwjj86cxyzxv5/eACg3WKek!mFP8CfkZDsJD5XkVCkIE0ekxuIw=
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
 by: Javier - Wed, 7 Jun 2023 18:39 UTC

Kaz Kylheku <864-117-4973@kylheku.com> wrote:
> Bash has a computed variable syntax. If there is a $word variable which
> holds foo, then ${!word} will produce expansion $foo.

BTW, does anybody know why the shell does not allow the nested
brace syntax ${${variable}} for indirect variable reference?

$ a=b
$ b=c
$ echo ${${a}}
-bash: ${${a}}: bad substitution

It would make sense, as it is the most intuitive syntax.
In perl the nested brace syntax works alright

$ perl -e '${a}="b"; ${b}="c"; print "${${a}}\n"'
c

Re: Bash ${!word} computed variable syntax.

<slrnu81mdg.3135.naddy@lorvorc.mips.inka.de>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=6332&group=comp.unix.shell#6332

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!news.szaf.org!inka.de!mips.inka.de!.POSTED.localhost!not-for-mail
From: nad...@mips.inka.de (Christian Weisgerber)
Newsgroups: comp.unix.shell
Subject: Re: Bash ${!word} computed variable syntax.
Date: Wed, 7 Jun 2023 19:25:36 -0000 (UTC)
Message-ID: <slrnu81mdg.3135.naddy@lorvorc.mips.inka.de>
References: <20230605202611.371@kylheku.com>
<D_6cnVNjRel7Ux35nZ2dnZfqnPudnZ2d@brightview.co.uk>
Injection-Date: Wed, 7 Jun 2023 19:25:36 -0000 (UTC)
Injection-Info: lorvorc.mips.inka.de; posting-host="localhost:::1";
logging-data="99430"; mail-complaints-to="usenet@mips.inka.de"
User-Agent: slrn/1.0.3 (FreeBSD)
 by: Christian Weisgerber - Wed, 7 Jun 2023 19:25 UTC

On 2023-06-07, Javier <invalid@invalid.invalid> wrote:

> BTW, does anybody know why the shell does not allow the nested
> brace syntax ${${variable}} for indirect variable reference?

Presumably because it does a single pass of variable substitutions.
The result of a variable substitution is not subject to variable
substitution again.

--
Christian "naddy" Weisgerber naddy@mips.inka.de

Re: Bash ${!word} computed variable syntax.

<20230607132616.74@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=6333&group=comp.unix.shell#6333

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 864-117-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.shell
Subject: Re: Bash ${!word} computed variable syntax.
Date: Wed, 7 Jun 2023 20:34:21 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 71
Message-ID: <20230607132616.74@kylheku.com>
References: <20230605202611.371@kylheku.com>
<D_6cnVNjRel7Ux35nZ2dnZfqnPudnZ2d@brightview.co.uk>
Injection-Date: Wed, 7 Jun 2023 20:34:21 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="74798bd1e9a9586c9013e16b583adaae";
logging-data="1300770"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18uxbQ6/a11jU61+Vs8TpVW47soha71CIQ="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:SLHX1uVNYE35D+2Tt4wfLymJvkg=
 by: Kaz Kylheku - Wed, 7 Jun 2023 20:34 UTC

On 2023-06-07, Javier <invalid@invalid.invalid> wrote:
> Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>> Bash has a computed variable syntax. If there is a $word variable which
>> holds foo, then ${!word} will produce expansion $foo.
>
> BTW, does anybody know why the shell does not allow the nested
> brace syntax ${${variable}} for indirect variable reference?

That's exactly why I was investigating into it.

The feature is present, together with arrays and ${name[@]} and all
that in bash-2.0.

The feature is absent, as are arrays and all, in bash-1.14.17.

The GNU FTP archive has no tarball of Bash between those two
versions!

I was trying to find out whether ${!word} was introduced before,
after, or at the same time as ${!name[@]} and those other ${!...}
notations.

So, as far as I can guess, Ramey (or someone) perhaps introduced a ${!
sequence for signaling the start of some expansion extensions. Computed
names ended up there, whether as a first feature, or later one.

To learn more, we would have to bother Ramey with questions.

> $ a=b
> $ b=c
> $ echo ${${a}}
> -bash: ${${a}}: bad substitution

Consider that this would be crippled if it only supported ${$.

Programmers would expect it to work like computed variables in Make,
which supports:

OBJS_$(this_file) = foo.o

Such that you can then reference:

$(OBJS_$(file))

People would try ${abc_$foo} and expect that to work too,
not just ${$foo}.

Whereas ${!...} can more easily handwaved away as the ${! being
a canned sequence for introducing a bunch of extensions (that
are not even related).

> It would make sense, as it is the most intuitive syntax.
> In perl the nested brace syntax works alright
>
> $ perl -e '${a}="b"; ${b}="c"; print "${${a}}\n"'

Possibly, it adds a cost. The shell implementation would have to scan
the interior of every simple ${var} reference for dollar sign
expansions. Rather than just accumulating the characters and pumping the
resulting identifier through a hash table.

(That cost can be addressed by compiling the notation: doing the
scan when the syntax is processed, not every time the surrounding
function is called or loop body executed. So it's not an entirely
valid objection; but it does somewhat raise the bar for a good
implementation.)

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: Bash ${!word} computed variable syntax.

<20230607133434.493@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=6334&group=comp.unix.shell#6334

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 864-117-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.shell
Subject: Re: Bash ${!word} computed variable syntax.
Date: Wed, 7 Jun 2023 20:39:29 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 34
Message-ID: <20230607133434.493@kylheku.com>
References: <20230605202611.371@kylheku.com>
<D_6cnVNjRel7Ux35nZ2dnZfqnPudnZ2d@brightview.co.uk>
<slrnu81mdg.3135.naddy@lorvorc.mips.inka.de>
Injection-Date: Wed, 7 Jun 2023 20:39:29 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="74798bd1e9a9586c9013e16b583adaae";
logging-data="1300770"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/ys0XVMYLRyXHEvkSs+5C6ZxD8hBSZ8lI="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:4dEP9CSvxWy8WE0Fyt5XKgjJp98=
 by: Kaz Kylheku - Wed, 7 Jun 2023 20:39 UTC

On 2023-06-07, Christian Weisgerber <naddy@mips.inka.de> wrote:
> On 2023-06-07, Javier <invalid@invalid.invalid> wrote:
>
>> BTW, does anybody know why the shell does not allow the nested
>> brace syntax ${${variable}} for indirect variable reference?
>
> Presumably because it does a single pass of variable substitutions.
> The result of a variable substitution is not subject to variable
> substitution again.

But that would still be the case here. If the inner ${variable}
happened to produce the text "foo$a" that would no longer be
scanned to replace $a; it would be inserted as ${foo$a}, and
resolved by looking for the variable name foo$a. (Or indicating
an error due to a bad variable name.)

Note that expansions *are* performed in some ${...} syntaxes.
For isntance in ${foo%bar}, bar is subject to substitutions!

Long-winded way to do ${TERM%r}:

$ echo $TERM
screen-256color
$ echo ${TERM%$(echo -n R | tr '[A-Z]' '[a-z]')}
screen-256colo

In principle, the left hand side of % could also be processed
for expansions in the same way. Thus, the whole interior of
${...} when there is no % operator.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: Bash ${!word} computed variable syntax.

<u5r0st$18jq2$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=6335&group=comp.unix.shell#6335

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: Bash ${!word} computed variable syntax.
Date: Thu, 8 Jun 2023 00:38:20 +0200
Organization: A noiseless patient Spider
Lines: 52
Message-ID: <u5r0st$18jq2$1@dont-email.me>
References: <20230605202611.371@kylheku.com>
<D_6cnVNjRel7Ux35nZ2dnZfqnPudnZ2d@brightview.co.uk>
<20230607132616.74@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 7 Jun 2023 22:38:21 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="fef1ce370547172238fcbf51a0c124b5";
logging-data="1331010"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19HtNwNE/tOYqNbaEqmL8TF"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:h3MS0cXVIiOE5zRhg/iLj7cGG8o=
In-Reply-To: <20230607132616.74@kylheku.com>
X-Enigmail-Draft-Status: N1110
 by: Janis Papanagnou - Wed, 7 Jun 2023 22:38 UTC

On 07.06.2023 22:34, Kaz Kylheku wrote:
> On 2023-06-07, Javier <invalid@invalid.invalid> wrote:
>> Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>>> Bash has a computed variable syntax. If there is a $word variable which
>>> holds foo, then ${!word} will produce expansion $foo.
>>
>> BTW, does anybody know why the shell does not allow the nested
>> brace syntax ${${variable}} for indirect variable reference?

Because it's non-POSIX ? - Normative text is spread across the
specs, you'll probably have to read the whole chapter 2.6 on
Word Expansion, but see also 'parameter' vs. 'word' definitions
in the syntax.

>
> That's exactly why I was investigating into it.
>
> The feature is present, together with arrays and ${name[@]} and all
> that in bash-2.0.
>
> The feature is absent, as are arrays and all, in bash-1.14.17.
>
> The GNU FTP archive has no tarball of Bash between those two
> versions!
>
> I was trying to find out whether ${!word} was introduced before,
> after, or at the same time as ${!name[@]} and those other ${!...}
> notations.

All I can say is that 'typeset -n nameref' and '${!nameref}' were
introduced with ksh93 at times when bash did not support namerefs.
The same with variable patterns, e.g. as in ${!HIST*}, or the
array subscripts ${!arr[@]} etc.

>
> So, as far as I can guess, Ramey (or someone) perhaps introduced a ${!
> sequence for signaling the start of some expansion extensions. Computed
> names ended up there, whether as a first feature, or later one.

I _think_ (but may be misremembering) that bash supported ${!var}
before it supported namerefs, and after ksh used that syntax. But
ksh does not support the indirection ${!var} with bash semantic.
So most of the ${!...} semantics seems to have been borrowed by
bash, with the exception of the [non-nameref] var-indirection.

>
> To learn more, we would have to bother Ramey with questions.
>
[...]

Janis

Re: Bash ${!word} computed variable syntax.

<20230607164431.477@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=6336&group=comp.unix.shell#6336

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 864-117-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.shell
Subject: Re: Bash ${!word} computed variable syntax.
Date: Wed, 7 Jun 2023 23:53:35 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 56
Message-ID: <20230607164431.477@kylheku.com>
References: <20230605202611.371@kylheku.com>
<D_6cnVNjRel7Ux35nZ2dnZfqnPudnZ2d@brightview.co.uk>
<20230607132616.74@kylheku.com> <u5r0st$18jq2$1@dont-email.me>
Injection-Date: Wed, 7 Jun 2023 23:53:35 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="59069b8d8ec10bd154ca39edd1f8f5c9";
logging-data="1342702"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+OORSLPbflmFJGbAon1S4gq5xPGNW228c="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:jlldcwm3R49vs5hwdvCAnAJGk/Y=
 by: Kaz Kylheku - Wed, 7 Jun 2023 23:53 UTC

On 2023-06-07, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
> On 07.06.2023 22:34, Kaz Kylheku wrote:
>> On 2023-06-07, Javier <invalid@invalid.invalid> wrote:
>>> Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>>>> Bash has a computed variable syntax. If there is a $word variable which
>>>> holds foo, then ${!word} will produce expansion $foo.
>>>
>>> BTW, does anybody know why the shell does not allow the nested
>>> brace syntax ${${variable}} for indirect variable reference?
>
> Because it's non-POSIX ? - Normative text is spread across the
> specs, you'll probably have to read the whole chapter 2.6 on
> Word Expansion, but see also 'parameter' vs. 'word' definitions
> in the syntax.

Do you meant that supporting expansions in the variable name
would violate POSIX (be a non-conforming extension that would have
to be disabled in POSIX mode?)

Even that wouldn't necessarily stop it, unless it broke a lot of
code in practice.

If ${a$b} happens to be not well-formed according to POSIX,
then the only thing wrong with making it work is that we are
not loudly diagnosing the error.

>> So, as far as I can guess, Ramey (or someone) perhaps introduced a ${!
>> sequence for signaling the start of some expansion extensions. Computed
>> names ended up there, whether as a first feature, or later one.
>
> I _think_ (but may be misremembering) that bash supported ${!var}
> before it supported namerefs

This is correct. The bash-2.0 archive has ${!var}, as I mentioned.

Furthermore, it has this:

14) Which new features in ksh-93 are not in bash, and which are?

New things in ksh-93 not in bash-2.0:
associative arrays
floating point arithmetic
++, --, comma arithmetic operators
math library functions
${!name[sub]} name of subscript for associative array
${!prefix*} and {!prefix@} variable name prefix expansions
`.' is allowed in variable names to create a hierarchical namespace
more extensive compound assignment syntax
discipline functions
`sleep' and `getconf' builtins (bash has loadable versions)
***** typeset -n and `nameref' variables *****

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: Bash ${!word} computed variable syntax.

<DH6dncXoCZ2QhBz5nZ2dnZfqnPSdnZ2d@brightview.co.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=6337&group=comp.unix.shell#6337

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border-2.nntp.ord.giganews.com!nntp.giganews.com!Xl.tags.giganews.com!local-1.nntp.ord.giganews.com!nntp.brightview.co.uk!news.brightview.co.uk.POSTED!not-for-mail
NNTP-Posting-Date: Wed, 07 Jun 2023 23:55:57 +0000
From: inva...@invalid.invalid (Javier)
Subject: Re: Bash ${!word} computed variable syntax.
Newsgroups: comp.unix.shell
References: <20230605202611.371@kylheku.com> <D_6cnVNjRel7Ux35nZ2dnZfqnPudnZ2d@brightview.co.uk> <20230607132616.74@kylheku.com>
Message-ID: <DH6dncXoCZ2QhBz5nZ2dnZfqnPSdnZ2d@brightview.co.uk>
Date: Wed, 07 Jun 2023 23:55:57 +0000
Lines: 40
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-fCl+XtPEDRV49DS7X/ogS7qcIyS6v/vkxlfiG5R1xPAPGcMdQ0IHN1x9OoE4oNz8P/vGkX/MQ8ByZ4C!lyXyAL6wfelbO+Rw/JgOuojqcSqdvTYyZHKFylKaXacsyCWDpjkDK9FNi+iO/Nliw1tPH9s7ZhiJ!redk+ofq9p7/Z5vSOlBV1YuaDEo=
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
 by: Javier - Wed, 7 Jun 2023 23:55 UTC

Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>> $ a=b
>> $ b=c
>> $ echo ${${a}}
>> -bash: ${${a}}: bad substitution
>
> Consider that this would be crippled if it only supported ${$.
>
> ...
>
> People would try ${abc_$foo} and expect that to work too,
> not just ${$foo}.

Another problem with the ${abc_${foo}} syntax would be that it would
complicate the specification for typing variables with the NAMEREF
attribute in the builtin 'declare'

declare: declare [-aAfFgilnrtux] [-p] [name[=value] ...]
-n make NAME a reference to the variable named by its value

> Whereas ${!...} can more easily handwaved away as the ${! being
> a canned sequence for introducing a bunch of extensions (that
> are not even related).

The choice of syntax for those extensions doesn't look coherent.
${!var} is for getting variable contents, while ${!var*} is for
getting variable names. I wouldn't call that a good syntax design.

'${!PREFIX*}'
'${!PREFIX@}'
Expands to the names of variables whose names begin with PREFIX,
...

'${!NAME[@]}'
'${!NAME[*]}'
If NAME is an array variable, expands to the list of array indices
(keys) assigned in NAME.
...

In any case the syntax seems to come from the ksh designers, not Chet Ramey.

Re: Bash ${!word} computed variable syntax.

<20230607170737.156@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=6338&group=comp.unix.shell#6338

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 864-117-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.shell
Subject: Re: Bash ${!word} computed variable syntax.
Date: Thu, 8 Jun 2023 00:18:53 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 40
Message-ID: <20230607170737.156@kylheku.com>
References: <20230605202611.371@kylheku.com>
<D_6cnVNjRel7Ux35nZ2dnZfqnPudnZ2d@brightview.co.uk>
<20230607132616.74@kylheku.com>
<DH6dncXoCZ2QhBz5nZ2dnZfqnPSdnZ2d@brightview.co.uk>
Injection-Date: Thu, 8 Jun 2023 00:18:53 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="59069b8d8ec10bd154ca39edd1f8f5c9";
logging-data="1348622"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+gGm24Lv4Gqhwr0Hs5XCouoJZJ5UEqlVA="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:Rbn7wWiZLG2LwTQRNMIf3e1qGnw=
 by: Kaz Kylheku - Thu, 8 Jun 2023 00:18 UTC

On 2023-06-07, Javier <invalid@invalid.invalid> wrote:
> Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>>> $ a=b
>>> $ b=c
>>> $ echo ${${a}}
>>> -bash: ${${a}}: bad substitution
>>
>> Consider that this would be crippled if it only supported ${$.
>>
>> ...
>>
>> People would try ${abc_$foo} and expect that to work too,
>> not just ${$foo}.
>
> Another problem with the ${abc_${foo}} syntax would be that it would
> complicate the specification for typing variables with the NAMEREF
> attribute in the builtin 'declare'
>
> declare: declare [-aAfFgilnrtux] [-p] [name[=value] ...]
> -n make NAME a reference to the variable named by its value

I don't see the huge issue. You calculate the name like abc_$foo ->
abc_xyz. Then if abc_xyz is declared as a nameref, you dereference it;
that part is basically independent and transparent.

Bash's ${!word} specification makes a big deal about whether word itself
is a nameref. The behavior splits. If it's not a nameref, you get the
named variable indirection. If word it is a nameref, then the feature
retrieves the name of the variable targeted by word. Completely
unrelated, different semantics, ouch.

Nothing is mentioned about the case when the target variable of {$!word}
is a nameref (and $word isn't); presumably that works. We dereference to
the nameref, which then dereferences to its target, the way we
would want ${abc_$foo} to work.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: Bash ${!word} computed variable syntax.

<xsudnYgQltRjvxz5nZ2dnZfqnPWdnZ2d@brightview.co.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=6339&group=comp.unix.shell#6339

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!feeder.usenetexpress.com!tr3.iad1.usenetexpress.com!69.80.99.23.MISMATCH!Xl.tags.giganews.com!local-2.nntp.ord.giganews.com!nntp.brightview.co.uk!news.brightview.co.uk.POSTED!not-for-mail
NNTP-Posting-Date: Thu, 08 Jun 2023 00:38:22 +0000
From: inva...@invalid.invalid (Javier)
Subject: Re: Bash ${!word} computed variable syntax.
Newsgroups: comp.unix.shell
References: <20230605202611.371@kylheku.com> <D_6cnVNjRel7Ux35nZ2dnZfqnPudnZ2d@brightview.co.uk> <20230607132616.74@kylheku.com> <u5r0st$18jq2$1@dont-email.me>
Message-ID: <xsudnYgQltRjvxz5nZ2dnZfqnPWdnZ2d@brightview.co.uk>
Date: Thu, 08 Jun 2023 00:38:22 +0000
Lines: 19
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-JypfXhOBxb1GYc15G4nzkSWllt93aqHTFv1OmEkNj8QhDeEWLHpvsXOYh5ZXxj3VQnvQVz4Nr8GaFpV!UirT8fYAfgRvPRYnZahDG74LleOeZUwA+BIPXP+6mZdyf3RPvKC58nDHIxk6NKbfUmsyY4JoXW7W!SH0vL4RTQJ1KYEzNbWQpwzr1oBg=
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
 by: Javier - Thu, 8 Jun 2023 00:38 UTC

Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
> On 07.06.2023 22:34, Kaz Kylheku wrote:
>> On 2023-06-07, Javier <invalid@invalid.invalid> wrote:
>>> Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>>>> Bash has a computed variable syntax. If there is a $word variable which
>>>> holds foo, then ${!word} will produce expansion $foo.
>>>
>>> BTW, does anybody know why the shell does not allow the nested
>>> brace syntax ${${variable}} for indirect variable reference?
>
> Because it's non-POSIX ? - Normative text is spread across the
> specs, you'll probably have to read the whole chapter 2.6 on
> Word Expansion, but see also 'parameter' vs. 'word' definitions
> in the syntax.

Supporting the ${${variable}} syntax would not stop Bash from being
able to execute correctly POSIX shell code. POSIX-compliant shells
can implement non-POSIX extensions, and even exists the possibility
that those extensions be adopted by POSIX in the future.

Re: Bash ${!word} computed variable syntax.

<T3OdnTv9BscJuRz5nZ2dnZfqn_WdnZ2d@brightview.co.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=6340&group=comp.unix.shell#6340

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border-2.nntp.ord.giganews.com!nntp.giganews.com!Xl.tags.giganews.com!local-1.nntp.ord.giganews.com!nntp.brightview.co.uk!news.brightview.co.uk.POSTED!not-for-mail
NNTP-Posting-Date: Thu, 08 Jun 2023 00:45:08 +0000
From: inva...@invalid.invalid (Javier)
Subject: Re: Bash ${!word} computed variable syntax.
Newsgroups: comp.unix.shell
References: <20230605202611.371@kylheku.com> <D_6cnVNjRel7Ux35nZ2dnZfqnPudnZ2d@brightview.co.uk> <20230607132616.74@kylheku.com> <u5r0st$18jq2$1@dont-email.me>
Message-ID: <T3OdnTv9BscJuRz5nZ2dnZfqn_WdnZ2d@brightview.co.uk>
Date: Thu, 08 Jun 2023 00:45:08 +0000
Lines: 23
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-OtNHdtDHQlznhotldqURQGPKP4MD5bE+wFLg5lsUazVdK/yzT2lL6BUpjmXNoPQJT8lLPEV0uryLFeP!apZEqnocBORkhxOaLhcrnTQ9jiJMyUqk2ECjLQBOSF6EqxEQXe/XlxIb/iVmpZ/iNMXi2gLuQSgM!GAprT3ymk2MMVdjhm9q2W1sGIOc=
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
 by: Javier - Thu, 8 Jun 2023 00:45 UTC

Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
> Because it's non-POSIX ?

Also, since you mention non-POSIXness, I checked whether the nested
variable reference syntax is supported in zsh, which does not attempt
to be POSIX compliant.

To my surprise this is what it happens in zsh:

a="b"; b="c"; printf "${${a}}\n";
b

I checked the zsh docs and it needs a parameter expansion flag (P) to
get the expected result:

a="b"; b="c"; printf "${(P)${a}}\n";
c

This looks like an awful syntax design in zsh, and can lead to very
hard to detect bugs, especially for people familiar with perl

perl -e '${a}="b"; ${b}="c"; print "${${a}}\n"'
c

Re: Bash ${!word} computed variable syntax.

<u5r9e0$19boi$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=6341&group=comp.unix.shell#6341

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: Bash ${!word} computed variable syntax.
Date: Thu, 8 Jun 2023 03:03:59 +0200
Organization: A noiseless patient Spider
Lines: 28
Message-ID: <u5r9e0$19boi$1@dont-email.me>
References: <20230605202611.371@kylheku.com>
<D_6cnVNjRel7Ux35nZ2dnZfqnPudnZ2d@brightview.co.uk>
<20230607132616.74@kylheku.com> <u5r0st$18jq2$1@dont-email.me>
<xsudnYgQltRjvxz5nZ2dnZfqnPWdnZ2d@brightview.co.uk>
MIME-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 8 Jun 2023 01:04:00 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="d4fc3324255003d35d0d3f1df0dc9906";
logging-data="1355538"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+y2y+sqqdH/FgzGEiwMoRg"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:bDf3xkzeln1OaTGJJM7B3ROwHD4=
In-Reply-To: <xsudnYgQltRjvxz5nZ2dnZfqnPWdnZ2d@brightview.co.uk>
 by: Janis Papanagnou - Thu, 8 Jun 2023 01:03 UTC

On 08.06.2023 02:38, Javier wrote:
> Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
>> On 07.06.2023 22:34, Kaz Kylheku wrote:
>>> On 2023-06-07, Javier <invalid@invalid.invalid> wrote:
>>>> Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>>>>> Bash has a computed variable syntax. If there is a $word variable which
>>>>> holds foo, then ${!word} will produce expansion $foo.
>>>>
>>>> BTW, does anybody know why the shell does not allow the nested
>>>> brace syntax ${${variable}} for indirect variable reference?
>>
>> Because it's non-POSIX ? - Normative text is spread across the
>> specs, you'll probably have to read the whole chapter 2.6 on
>> Word Expansion, but see also 'parameter' vs. 'word' definitions
>> in the syntax.
>
> Supporting the ${${variable}} syntax would not stop Bash from being
> able to execute correctly POSIX shell code. POSIX-compliant shells
> can implement non-POSIX extensions, and even exists the possibility
> that those extensions be adopted by POSIX in the future.

What I meant was that in POSIX there's some definitions what may
and what may not appear in lexical context - that's what I thought
to have seen in chapter 2.6 -, and I did not mean any lexically
non-related extensions.

Janis

Re: Bash ${!word} computed variable syntax.

<u5ra1r$19d6m$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=6342&group=comp.unix.shell#6342

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: Bash ${!word} computed variable syntax.
Date: Thu, 8 Jun 2023 03:14:34 +0200
Organization: A noiseless patient Spider
Lines: 14
Message-ID: <u5ra1r$19d6m$1@dont-email.me>
References: <20230605202611.371@kylheku.com>
<D_6cnVNjRel7Ux35nZ2dnZfqnPudnZ2d@brightview.co.uk>
<20230607132616.74@kylheku.com>
<DH6dncXoCZ2QhBz5nZ2dnZfqnPSdnZ2d@brightview.co.uk>
<20230607170737.156@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=windows-1252
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 8 Jun 2023 01:14:35 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="17f966df92fb35b1f75e50543325c07f";
logging-data="1357014"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+VI1x3ocx9UMLM89mxTs/A"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101
Thunderbird/45.8.0
Cancel-Lock: sha1:NaAimQRMLGbpaFiSXp2DBtoSukY=
In-Reply-To: <20230607170737.156@kylheku.com>
 by: Janis Papanagnou - Thu, 8 Jun 2023 01:14 UTC

On 08.06.2023 02:18, Kaz Kylheku wrote:
>
> Bash's ${!word} specification makes a big deal about whether word itself
> is a nameref. The behavior splits. If it's not a nameref, you get the
> named variable indirection. If word it is a nameref, then the feature
> retrieves the name of the variable targeted by word. Completely
> unrelated, different semantics, ouch.

Indeed. - What they seem to try to do is finding any non-conflicting
combination of punctuation characters to implement new features. (A
bit of a cynical view, I admit.)

Janis

Re: Bash ${!word} computed variable syntax.

<jgidncsw4-oZsRz5nZ2dnZfqn_WdnZ2d@brightview.co.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=6343&group=comp.unix.shell#6343

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border-2.nntp.ord.giganews.com!nntp.giganews.com!Xl.tags.giganews.com!local-1.nntp.ord.giganews.com!nntp.brightview.co.uk!news.brightview.co.uk.POSTED!not-for-mail
NNTP-Posting-Date: Thu, 08 Jun 2023 01:19:00 +0000
From: inva...@invalid.invalid (Javier)
Subject: Re: Bash ${!word} computed variable syntax.
Newsgroups: comp.unix.shell
References: <20230605202611.371@kylheku.com> <D_6cnVNjRel7Ux35nZ2dnZfqnPudnZ2d@brightview.co.uk> <20230607132616.74@kylheku.com> <DH6dncXoCZ2QhBz5nZ2dnZfqnPSdnZ2d@brightview.co.uk> <20230607170737.156@kylheku.com>
Message-ID: <jgidncsw4-oZsRz5nZ2dnZfqn_WdnZ2d@brightview.co.uk>
Date: Thu, 08 Jun 2023 01:19:00 +0000
Lines: 18
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-BV3y5vUIETeRncnFFbjD+t3aNdWSVBAaEKSO413UZ+kxXarU4ale3LsZeXmvD6sL+APk0hoAkb6xmLJ!bph4FsSyXCPpv4rV8V6d99LXZ0mCQj0j46OVmv79UUFhIcNCPTxVukmmwJ2jytb8gbsApypfWhdV!oJSdz5CgQpODeZJwP+MlnyOPlcE=
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
 by: Javier - Thu, 8 Jun 2023 01:19 UTC

Kaz Kylheku <864-117-4973@kylheku.com> wrote:
> Bash's ${!word} specification makes a big deal about whether word itself
> is a nameref. The behavior splits. If it's not a nameref, you get the
> named variable indirection. If word it is a nameref, then the feature
> retrieves the name of the variable targeted by word. Completely
> unrelated, different semantics, ouch.

You are right, the behavior is different and I wasn't aware of it until
I read your message and tested the behavior myself with an example:

$ a=b; b=c; printf "${!a}\n";
c

$ declare -n a=b; b=c; printf "${!a}\n";
b

It's an awful design for a shell feature that was intended to prevent bugs.
Instead of preventing bugs it creates them.

Re: Bash ${!word} computed variable syntax.

<JOednVE7nbv9qhz5nZ2dnZfqn_WdnZ2d@brightview.co.uk>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=6344&group=comp.unix.shell#6344

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border-2.nntp.ord.giganews.com!nntp.giganews.com!Xl.tags.giganews.com!local-1.nntp.ord.giganews.com!nntp.brightview.co.uk!news.brightview.co.uk.POSTED!not-for-mail
NNTP-Posting-Date: Thu, 08 Jun 2023 02:05:20 +0000
From: inva...@invalid.invalid (Javier)
Subject: Re: Bash ${!word} computed variable syntax.
Newsgroups: comp.unix.shell
References: <20230605202611.371@kylheku.com> <D_6cnVNjRel7Ux35nZ2dnZfqnPudnZ2d@brightview.co.uk> <20230607132616.74@kylheku.com> <u5r0st$18jq2$1@dont-email.me> <xsudnYgQltRjvxz5nZ2dnZfqnPWdnZ2d@brightview.co.uk> <u5r9e0$19boi$1@dont-email.me>
Message-ID: <JOednVE7nbv9qhz5nZ2dnZfqn_WdnZ2d@brightview.co.uk>
Date: Thu, 08 Jun 2023 02:05:20 +0000
Lines: 31
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-BT6DvkOWSBfvjmkoX2AKvRGHXM8WKp6qQYPMsA8IJX39Snq0mwMaRbWLBgOdbJvWekWBywgSsg+jJtP!YjI7hrElF77X+izUSqo6gFyGmwjQcvH+A0xQyKJtF8unt4YUCCLvHfpnI4xLMxSsfa95QRpOZIIK!OxkEjinURke2iySwxN+5aIOU1wY=
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
 by: Javier - Thu, 8 Jun 2023 02:05 UTC

Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
> On 08.06.2023 02:38, Javier wrote:
>> Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
>>> On 07.06.2023 22:34, Kaz Kylheku wrote:
>>>> On 2023-06-07, Javier <invalid@invalid.invalid> wrote:
>>>>> Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>>>>>> Bash has a computed variable syntax. If there is a $word variable which
>>>>>> holds foo, then ${!word} will produce expansion $foo.
>>>>>
>>>>> BTW, does anybody know why the shell does not allow the nested
>>>>> brace syntax ${${variable}} for indirect variable reference?
>>>
>>> Because it's non-POSIX ? - Normative text is spread across the
>>> specs, you'll probably have to read the whole chapter 2.6 on
>>> Word Expansion, but see also 'parameter' vs. 'word' definitions
>>> in the syntax.
>>
>> Supporting the ${${variable}} syntax would not stop Bash from being
>> able to execute correctly POSIX shell code. POSIX-compliant shells
>> can implement non-POSIX extensions, and even exists the possibility
>> that those extensions be adopted by POSIX in the future.
>
> What I meant was that in POSIX there's some definitions what may
> and what may not appear in lexical context - that's what I thought
> to have seen in chapter 2.6 -, and I did not mean any lexically
> non-related extensions.

https://pubs.opengroup.org/onlinepubs/9699919799.2018edition/utilities/V3_chap02.html#tag_18_06

I don't see anything in section 2.6 forbidding the nested ${${variable}} syntax.
Also, the ${!word} does not appear in the POSIX specification.

Re: Bash ${!word} computed variable syntax.

<87pm66il67.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=6345&group=comp.unix.shell#6345

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.unix.shell
Subject: Re: Bash ${!word} computed variable syntax.
Date: Wed, 07 Jun 2023 22:21:20 -0700
Organization: None to speak of
Lines: 32
Message-ID: <87pm66il67.fsf@nosuchdomain.example.com>
References: <20230605202611.371@kylheku.com>
<D_6cnVNjRel7Ux35nZ2dnZfqnPudnZ2d@brightview.co.uk>
<20230607132616.74@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="91c610a42a483af4c8581264c28badd5";
logging-data="1515411"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+M3VsvDSaj0Hb0geTixrPJ"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:KBdy7GxKD4VqsAE/ab0k7zyGPVs=
sha1:WcVssWl5TGsdVlu9XiVysrm0HVU=
 by: Keith Thompson - Thu, 8 Jun 2023 05:21 UTC

Kaz Kylheku <864-117-4973@kylheku.com> writes:
> On 2023-06-07, Javier <invalid@invalid.invalid> wrote:
>> Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>>> Bash has a computed variable syntax. If there is a $word variable which
>>> holds foo, then ${!word} will produce expansion $foo.
>>
>> BTW, does anybody know why the shell does not allow the nested
>> brace syntax ${${variable}} for indirect variable reference?
>
> That's exactly why I was investigating into it.
>
> The feature is present, together with arrays and ${name[@]} and all
> that in bash-2.0.
>
> The feature is absent, as are arrays and all, in bash-1.14.17.
>
> The GNU FTP archive has no tarball of Bash between those two
> versions!
>
> I was trying to find out whether ${!word} was introduced before,
> after, or at the same time as ${!name[@]} and those other ${!...}
> notations.
[...]

This answer on unix.stackexchange.com:
https://unix.stackexchange.com/a/403119/10454
includes links to some older bash tarballs that aren't on ftp.gnu.org.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */

Re: Bash ${!word} computed variable syntax.

<20230608103822.522@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=6347&group=comp.unix.shell#6347

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 864-117-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.shell
Subject: Re: Bash ${!word} computed variable syntax.
Date: Thu, 8 Jun 2023 17:41:11 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 27
Message-ID: <20230608103822.522@kylheku.com>
References: <20230605202611.371@kylheku.com>
<D_6cnVNjRel7Ux35nZ2dnZfqnPudnZ2d@brightview.co.uk>
<20230607132616.74@kylheku.com>
<DH6dncXoCZ2QhBz5nZ2dnZfqnPSdnZ2d@brightview.co.uk>
<20230607170737.156@kylheku.com> <u5ra1r$19d6m$1@dont-email.me>
Injection-Date: Thu, 8 Jun 2023 17:41:11 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="59069b8d8ec10bd154ca39edd1f8f5c9";
logging-data="1673065"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18xiKMWEeWgA3JQEUvS7lErKTyb3nOhjtU="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:HsK2vRVBRReHoWFsBnUTQTnEQ10=
 by: Kaz Kylheku - Thu, 8 Jun 2023 17:41 UTC

On 2023-06-08, Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
> On 08.06.2023 02:18, Kaz Kylheku wrote:
>>
>> Bash's ${!word} specification makes a big deal about whether word itself
>> is a nameref. The behavior splits. If it's not a nameref, you get the
>> named variable indirection. If word it is a nameref, then the feature
>> retrieves the name of the variable targeted by word. Completely
>> unrelated, different semantics, ouch.
>
> Indeed. - What they seem to try to do is finding any non-conflicting
> combination of punctuation characters to implement new features. (A
> bit of a cynical view, I admit.)

But in this case it's totally conflicting at that leverl:
${!word} versus ... ${!word}. The behavvior is distinguished by a
declared property of word, which, I think, in the worst case, is not
knowable until ${!word} is being executed at run-time.

${!word} could occur in a Bash function, which is followed by
a dot command that source a file which declares a nameref,
and then that function is called and must treat word accordingly.
(Right?)

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: Bash ${!word} computed variable syntax.

<20230608104644.372@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=6348&group=comp.unix.shell#6348

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 864-117-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.shell
Subject: Re: Bash ${!word} computed variable syntax.
Date: Thu, 8 Jun 2023 17:47:32 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 15
Message-ID: <20230608104644.372@kylheku.com>
References: <20230605202611.371@kylheku.com>
<D_6cnVNjRel7Ux35nZ2dnZfqnPudnZ2d@brightview.co.uk>
<20230607132616.74@kylheku.com> <u5r0st$18jq2$1@dont-email.me>
<T3OdnTv9BscJuRz5nZ2dnZfqn_WdnZ2d@brightview.co.uk>
Injection-Date: Thu, 8 Jun 2023 17:47:32 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="59069b8d8ec10bd154ca39edd1f8f5c9";
logging-data="1673065"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19FxZpPpsQvumuBYil0604JqChB88ofGbk="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:2Ng64LFfShkmWbz+WHqkeGTYwYk=
 by: Kaz Kylheku - Thu, 8 Jun 2023 17:47 UTC

On 2023-06-08, Javier <invalid@invalid.invalid> wrote:
> This looks like an awful syntax design in zsh, and can lead to very
> hard to detect bugs, especially for people familiar with perl
>
> perl -e '${a}="b"; ${b}="c"; print "${${a}}\n"'
> c

WHy don't you ask in the Bash mailing list: if someone made a
good quality patch to implement ${abc_${a}}, with documentation
and test cases, would they merge it?

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: Bash ${!word} computed variable syntax.

<87legtiy3d.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=6349&group=comp.unix.shell#6349

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.unix.shell
Subject: Re: Bash ${!word} computed variable syntax.
Date: Thu, 08 Jun 2023 11:54:30 -0700
Organization: None to speak of
Lines: 35
Message-ID: <87legtiy3d.fsf@nosuchdomain.example.com>
References: <20230605202611.371@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="91c610a42a483af4c8581264c28badd5";
logging-data="1688179"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+q9aqmkdskYYuARcDfl/6F"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:tdaeSfSUxNYygVw/eiiY0za5nqY=
sha1:vbM5/QVqAwetUzJoxIaEntUqnq8=
 by: Keith Thompson - Thu, 8 Jun 2023 18:54 UTC

Kaz Kylheku <864-117-4973@kylheku.com> writes:
> Curious situation.
>
> Bash has a computed variable syntax. If there is a $word variable which
> holds foo, then ${!word} will produce expansion $foo.
[...]

Something the manual doesn't directly address (or I've missed it) is
that a nameref can refer to a nameref, to arbitrarily many levels.

For example:
```
#!/bin/bash

declare -n var0=var1
declare -n var1=var2
declare -n var2=var3
declare -n var3=var4
declare -n var4=var5
var5=42

echo "var0=$var0"
echo "\${!var0}=${!var0}"
```

The output is:
```
var0=42
${!var0}=var5
```

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */

ksh indirection [was: Bash ${!word} computed variable syntax]

<keevnaF3ekdU1@mid.individual.net>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=6351&group=comp.unix.shell#6351

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!usenet.goja.nl.eu.org!3.eu.feeder.erje.net!feeder.erje.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: mart...@inlv.demon.nl (Martijn Dekker)
Newsgroups: comp.unix.shell
Subject: ksh indirection [was: Bash ${!word} computed variable syntax]
Date: Thu, 8 Jun 2023 22:32:26 +0100
Lines: 40
Message-ID: <keevnaF3ekdU1@mid.individual.net>
References: <20230605202611.371@kylheku.com>
<D_6cnVNjRel7Ux35nZ2dnZfqnPudnZ2d@brightview.co.uk>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: individual.net gTBI7iZx0FmeFK70F2+mow0kwnOnLVIb05rOpwbCavYthRVgg=
Cancel-Lock: sha1:seGlHlL8rLzW15053jeg9Em2m0c=
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0)
Gecko/20100101 Thunderbird/102.11.2
Content-Language: en-GB
In-Reply-To: <D_6cnVNjRel7Ux35nZ2dnZfqnPudnZ2d@brightview.co.uk>
 by: Martijn Dekker - Thu, 8 Jun 2023 21:32 UTC

Op 07-06-2023 om 19:39 schreef Javier:
> BTW, does anybody know why the shell does not allow the nested
> brace syntax ${${variable}} for indirect variable reference?

The abandoned 93v- beta of AT&T ksh93 (and hence also the derived and
equally abandoned AT&T ksh2020 project) supported a ${$variable} syntax
for indirection, though nested braces are a syntax error.

I've found multiple bugs in their implementation of it, the worst being:

- It malfunctions for indexed array subscripts other than 0, @ and *
(e.g., ${$var[2]}) and associative array subscripts other than @ and *
- It crashes when the variable name is a negative integer

But those problems might be fixable. I could try backporting and fixing
it for a future ksh 93u+m/1.1 release.

However, the functionality is redundant because namerefs already exist:

$ nameref foo=bar
$ bar=OK
$ echo $foo
OK

The new syntax would allow:

$ foo=bar
$ bar=OK
$ echo ${$foo}
OK

It takes just as many steps, so I don't really see the advantage.

--
|| modernish -- harness the shell
|| https://github.com/modernish/modernish
||
|| KornShell lives!
|| https://github.com/ksh93/ksh

Re: Bash ${!word} computed variable syntax.

<20230608181715.460@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=6352&group=comp.unix.shell#6352

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 864-117-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.shell
Subject: Re: Bash ${!word} computed variable syntax.
Date: Fri, 9 Jun 2023 01:43:51 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 153
Message-ID: <20230608181715.460@kylheku.com>
References: <20230605202611.371@kylheku.com>
<87legtiy3d.fsf@nosuchdomain.example.com>
Injection-Date: Fri, 9 Jun 2023 01:43:51 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="8975a7690a3368096766e183a7a8e902";
logging-data="1889603"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/PJShtGuBm1S8Wxx+cT3irwZv0NGsiARw="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:HSSHRBtDhPnC6sx+u+boep5fdsQ=
 by: Kaz Kylheku - Fri, 9 Jun 2023 01:43 UTC

On 2023-06-08, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
> Kaz Kylheku <864-117-4973@kylheku.com> writes:
>> Curious situation.
>>
>> Bash has a computed variable syntax. If there is a $word variable which
>> holds foo, then ${!word} will produce expansion $foo.
> [...]
>
> Something the manual doesn't directly address (or I've missed it) is
> that a nameref can refer to a nameref, to arbitrarily many levels.
> For example:
> ```
> #!/bin/bash
>
> declare -n var0=var1
> declare -n var1=var2
> declare -n var2=var3
> declare -n var3=var4
> declare -n var4=var5
> var5=42
>
> echo "var0=$var0"
> echo "\${!var0}=${!var0}"
> ```
>
> The output is:
> ```
> var0=42
> ${!var0}=var5

The obvious question there is, how late is the binding?

If I add this:

declare -n var2=xvar3
xvar3=73

echo "var0=$var0"
echo "\${!var0}=${!var0}"

var0=73
${!var0}=xvar3

You expect this because, look, the syntax is allowing you to bind
references to nonexistent variables. The reference stores the
name. References do not "slide" to the target variable.

The second question is: could the observed behavior be
produced, while the implementation is optimized.
So that is to say, reference var0 could (perhaps at the time it
is evaluated) cache the fact that it refers to var5, and
then on subdsequent evaluations refer to the cache.

"declare -n" statements could traverse the graph of
known namerefs and invalidate these caches.

Next question: how does this play with Bash's dynamic
scope?

One test is this: function-local override of
namerefs.

declare -n var0=var1
declare -n var1=var2
declare -n var2=var3
declare -n var3=var4
declare -n var4=var5
var5=42

out()
{ echo "var0=$var0"
echo "\${!var0}=${!var0}"
}

fun()
{ local var2
local xvar3=73
declare -n var2=xvar3

out
}

out
fun
out

Output:

var0=42
${!var0}=var5
var0=73
${!var0}=xvar3
var0=42
${!var0}=var5

Well, that passed. When the function terminates, the dynamic
scope is undone and the original nameref configuration is
restored.

Let's test something else. We have an ordinary non-nameref var0.
In a dynamic scope, we create the var0->var1->...->var5 cascade.
All those vars are declared local. What does var0 look like?

When that scope terminates, what then?

out()
{ echo "var0=$var0"
echo "\${!var0}=${!var0}"
}

with_cascaded_namerefs()
{ local var0 var1 var2 var3 var4 var5
declare -n var0=var1
declare -n var1=var2
declare -n var2=var3
declare -n var3=var4
declare -n var4=var5
var5=42

out
}

var0=ordinary

out

with_cascaded_namerefs

out

Output:

var0=ordinary
${!var0}=
var0=42
${!var0}=var5
var0=ordinary
${!var0}=

Also passed: var0 is overridden to nameref, then restored to ordinary.

(If there aren't test cases like that, there should be.)

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: ksh indirection [was: Bash ${!word} computed variable syntax]

<20230608184434.703@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=6353&group=comp.unix.shell#6353

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 864-117-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.unix.shell
Subject: Re: ksh indirection [was: Bash ${!word} computed variable syntax]
Date: Fri, 9 Jun 2023 01:51:51 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 79
Message-ID: <20230608184434.703@kylheku.com>
References: <20230605202611.371@kylheku.com>
<D_6cnVNjRel7Ux35nZ2dnZfqnPudnZ2d@brightview.co.uk>
<keevnaF3ekdU1@mid.individual.net>
Injection-Date: Fri, 9 Jun 2023 01:51:51 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="8975a7690a3368096766e183a7a8e902";
logging-data="1889603"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19fzLzXe7W6DoYdmtEUkx/ruyAxEoai+8w="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:iDWoFIKz3yVuCME+XY6nHyua+Jc=
 by: Kaz Kylheku - Fri, 9 Jun 2023 01:51 UTC

On 2023-06-08, Martijn Dekker <martijn@inlv.demon.nl> wrote:
> Op 07-06-2023 om 19:39 schreef Javier:
>> BTW, does anybody know why the shell does not allow the nested
>> brace syntax ${${variable}} for indirect variable reference?
>
> The abandoned 93v- beta of AT&T ksh93 (and hence also the derived and
> equally abandoned AT&T ksh2020 project) supported a ${$variable} syntax
> for indirection, though nested braces are a syntax error.
>
> I've found multiple bugs in their implementation of it, the worst being:
>
> - It malfunctions for indexed array subscripts other than 0, @ and *
> (e.g., ${$var[2]}) and associative array subscripts other than @ and *
> - It crashes when the variable name is a negative integer
>
> But those problems might be fixable. I could try backporting and fixing
> it for a future ksh 93u+m/1.1 release.
>
> However, the functionality is redundant because namerefs already exist:
>
> $ nameref foo=bar
> $ bar=OK
> $ echo $foo
> OK
>
> The new syntax would allow:
>
> $ foo=bar
> $ bar=OK
> $ echo ${$foo}
> OK
>
> It takes just as many steps, so I don't really see the advantage.

The advantage would be Make style computed names that aren't pure
name indirections: ${abc_${foo}} and such.

In Make, you can use that to construct information databases.
By using dot in a name, it looks like member referencing.

Let's use points: everyone's favorite example for structures.

p1.x = 0
p1.y = 1

p2.x = 2
p2.y = 3

Now if we have a variable $(P) that holds a point like p1 or p2,
we can do $($(P).x) and $($(P).y) to read the "fields".

If symbols can be machine generated, then eval-ed assignments
can simulate real dynamic objects.

point=$(gensym) # point takes on value like __g0037

eval "${point}_x=42" # evaluates __g0037_x=0

echo ${${gensym}_x} # prints 42

Maybe if variable interpolations are allowed in variable names,
they should be allowed in assignments too (in the left hand
side) without eval, just:

${point}_x=42

Works in Make:

OBJECTS_$(this_file) := foo.o # GNU Make eager assignment
OBJECTS_$(this_file) += bar.o

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Re: ksh indirection [was: Bash ${!word} computed variable syntax]

<u5ul80$1ross$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=6354&group=comp.unix.shell#6354

  copy link   Newsgroups: comp.unix.shell
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: janis_pa...@hotmail.com (Janis Papanagnou)
Newsgroups: comp.unix.shell
Subject: Re: ksh indirection [was: Bash ${!word} computed variable syntax]
Date: Fri, 9 Jun 2023 09:43:48 +0200
Organization: A noiseless patient Spider
Lines: 67
Message-ID: <u5ul80$1ross$1@dont-email.me>
References: <20230605202611.371@kylheku.com>
<D_6cnVNjRel7Ux35nZ2dnZfqnPudnZ2d@brightview.co.uk>
<keevnaF3ekdU1@mid.individual.net> <20230608184434.703@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Fri, 9 Jun 2023 07:44:00 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="bec611d7326039b3940ee57b3db7501e";
logging-data="1958812"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19GyPV+8hHo8EDny987gUHU"
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.11.0
Cancel-Lock: sha1:iF7A51P/Yzoh1+fcErof0x8SZ7c=
Content-Language: en-US
In-Reply-To: <20230608184434.703@kylheku.com>
 by: Janis Papanagnou - Fri, 9 Jun 2023 07:43 UTC

On 09.06.23 03:51, Kaz Kylheku wrote:
>
> Let's use points: everyone's favorite example for structures.
>
> p1.x = 0
> p1.y = 1
>
> p2.x = 2
> p2.y = 3
>
>
> Now if we have a variable $(P) that holds a point like p1 or p2,
> we can do $($(P).x) and $($(P).y) to read the "fields".

In Kornshell we may write code like that (for example)

typeset -T point=( x=0 y=0 )
point p1=( x=1 y=2 )
point p2=( x=3 y=4 )

print $p1 $p2
print ${p1.x} ${p1.y}
print ${p2.x} ${p2.y}

p1.x=5

typeset -n p3=p1
print $p3
print ${p3.x} ${p3.y}

producing this output

( x=1 y=2 ) ( x=3 y=4 )
1 2
3 4
( x=5 y=2 )
5 2

Accessing variables with dot-notation and namerefs for indirection.

>
> If symbols can be machine generated, then eval-ed assignments
> can simulate real dynamic objects.
>
> point=$(gensym) # point takes on value like __g0037
>
> eval "${point}_x=42" # evaluates __g0037_x=0
>
> echo ${${gensym}_x} # prints 42
>
>
> Maybe if variable interpolations are allowed in variable names,
> they should be allowed in assignments too (in the left hand
> side) without eval, just:
>
> ${point}_x=42

For the latter (AFAICT) you'd need eval in Kornshell. But for the
work with compound variables (as shown) it doesn't seem necessary.

>
> [...]

Janis

Pages:12
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor