bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: for loop over parameter expansion of array can miss resulted empty l


From: Greg Wooledge
Subject: Re: for loop over parameter expansion of array can miss resulted empty list
Date: Tue, 22 Mar 2022 23:16:57 -0400

On Tue, Mar 22, 2022 at 08:05:21PM -0700, L A Walsh wrote:

> > > > > Repeat-By:
> > > > >    Code: x=("/"); for i in "${x[@]%/}"; do echo "i is '$i'"; done
> > > > >    Result: none
> > > > >    Expected result: i is ''

> Sorry, I got sidetracked.  In this case it wouldn't matter,
> if you have no directories where you are running this,
> then nothing will match, and it
> will be a null (empty) expression. The
> for i in X [Y [Z]], statement will execute once for each
> non-null value after the 'in'.  If there are no expressions,
> then it won't execute.  Thus there should be no output.

The array 'x' has one element in it, so "${x[@]}" should expand to
one word.  Likewise, one would expect "${x[@]%whatever}" to expand to
a single word also, regardless of the actual content of the array, or
the value of 'whatever'.  Prior to bash 4.2, it did.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]