Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > gnu.bash.bug > #13771 > unrolled thread

Re: Double substitution issue

Started byWilliam Entriken <fulldecent@gmail.com>
First post2018-02-28 11:13 -0500
Last post2018-02-28 11:13 -0500
Articles 1 — 1 participant

Back to article view | Back to gnu.bash.bug

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Double substitution issue William Entriken <fulldecent@gmail.com> - 2018-02-28 11:13 -0500

#13771 — Re: Double substitution issue

FromWilliam Entriken <fulldecent@gmail.com>
Date2018-02-28 11:13 -0500
SubjectRe: Double substitution issue
Message-ID<mailman.9870.1519834444.27995.bug-bash@gnu.org>
Thank you for explaining this fine point.

I have found a way to use double expansion, and of course this is not safe
in all contexts:

    from=1;to=3;eval echo {$from..$to}

Regards,
Will


William Entriken
+1 267-738-4201


On Wed, Feb 28, 2018 at 10:03 AM, Chet Ramey <chet.ramey@case.edu> wrote:

> On 2/28/18 3:25 AM, William Entriken wrote:
> > This behavior is different in zsh and bash, and maybe bash behavior is a
> > bug.
> >
> > # Test case
> >
> > touch 1 2 3
> > cat > script.sh <<EOL
> > from=1
> > to=3
> > ls {$from..$to}
> > EOL
> > chmod a+x script.sh
> >
> > bash ./script.sh
> >
> > zsh ./script.sh
> >
> > # Expected
> >
> > Both list files 1, 2, 3
> >
> > # Actual
> >
> > zsh passes.
> >
> > Bash fails the chained substitution with:
> >
> > ls: {1..3}: No such file or directory
>
> This is how bash works, how it's always worked, and how it's documented to
> work:
>
> "Brace expansion is performed before any other expansions, and any char-
>  acters special to other expansions are preserved in the result.  It  is
>  strictly  textual.  Bash does not apply any syntactic interpretation to
>  the context of the expansion or the text between the braces."
>
> So you have {$from..$to}, which is not a valid sequence expression because
> $from and $to are not integers. Invalid brace expansions are left
> unchanged. When the word undergoes parameter expansion, you get {1..3}.
>
> --
> ``The lyf so short, the craft so long to lerne.'' - Chaucer
>                  ``Ars longa, vita brevis'' - Hippocrates
> Chet Ramey, UTech, CWRU    chet@case.edu    http://tiswww.cwru.edu/~chet/
>

[toc] | [standalone]


Back to top | Article view | gnu.bash.bug


csiph-web