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


Groups > gnu.bash.bug > #15481

Re: Is this a bug by any chance?

From Greg Wooledge <wooledg@eeg.ccf.org>
Newsgroups gnu.bash.bug
Subject Re: Is this a bug by any chance?
Date 2019-10-07 09:37 -0400
Message-ID <mailman.1225.1570455444.2651.bug-bash@gnu.org> (permalink)
References <99440807.4131727.1570301315442.ref@mail.yahoo.com> <99440807.4131727.1570301315442@mail.yahoo.com> <20191007124737.GM28751@eeg.ccf.org> <7892e473-d0d2-1207-8eb1-916b8571a496@passchier.net> <20191007133716.GR28751@eeg.ccf.org>

Show all headers | View raw


On Mon, Oct 07, 2019 at 08:05:12PM +0700, pepa65 wrote:
> > On Sat, Oct 05, 2019 at 06:48:35PM +0000, George R Goffe via Bug reports for the GNU Bourne Again SHell wrote:
> >> I was expecting to see:
> >> 12345
> > 
> > If you want to create a *list* and iterate over that list, one element
> > at a time, use arrays instead of string variables.
> > 
> > x=(1 2 3 4 5)
> > for z in "${x[@]}"; do
> >   echo "$z"
> > done
> 
> Just to be clear (as nobody has mentioned this) to get them all on the
> same line, you need "echo -n", and you can do this without using arrays:
> 
> x="1 2 3 4 5"
> for z in $x
> do echo -n "$z"
> done

This has some issues.  echo -n will fail if one of the list elements is
an option recognized by echo.  The unquoted $x expansion will fail if
one of the list elements is a glob that matches some files, or a glob
that matches no files if nullglob is active.

Also, the list-in-a-string gives you absolutely no way to handle list
elements that contain internal whitespace.  This is what arrays are for.
Please use the arrays.

x=(1 2 3 4 5 -n '/*' 'hello world')
for z in "${x[@]}"; do
  printf %s "$z"
done
echo

Back to gnu.bash.bug | Previous | Next | Find similar | Unroll thread


Thread

Re: Is this a bug by any chance? Greg Wooledge <wooledg@eeg.ccf.org> - 2019-10-07 09:37 -0400

csiph-web