Re: Is this a bug by any chance?

2019-10-07 Thread Greg Wooledge
On Mon, Oct 07, 2019 at 04:23:44PM +0200, Andreas Schwab wrote:
> On Okt 07 2019, Greg Wooledge  wrote:
> 
> > x=(1 2 3 4 5 -n '/*' 'hello world')
> > for z in "${x[@]}"; do
> >   printf %s "$z"
> > done
> > echo
> 
> (IFS=; printf '%s\n' "${x[*]}")

Or avoid the fork():

printf %s "${x[@]}"
echo

I preserved the original poster's loop, because it's probably
another !b9 question.



Re: Is this a bug by any chance?

2019-10-07 Thread Andreas Schwab
On Okt 07 2019, Greg Wooledge  wrote:

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

(IFS=; printf '%s\n' "${x[*]}")

Andreas.

-- 
Andreas Schwab, sch...@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."



Re: Is this a bug by any chance?

2019-10-07 Thread Greg Wooledge
On Mon, Oct 07, 2019 at 08:05:12PM +0700, pepa65 wrote:
> > On Sat, Oct 05, 2019 at 06:48:35PM +, 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



Re: Is this a bug by any chance?

2019-10-07 Thread pepa65
> On Sat, Oct 05, 2019 at 06:48:35PM +, 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

Cheers,
Peter






Re: Is this a bug by any chance?

2019-10-07 Thread Greg Wooledge
On Sat, Oct 05, 2019 at 06:48:35PM +, George R Goffe via Bug reports for 
the GNU Bourne Again SHell wrote:
> I was expecting to see:
> 12345

> #!./bash -xv
>  x="1 2 3 4 5"
>  for z in "$x"
>     do
>    echo "$z"
>     done
>  exit 0

Not a bug.  You've created a string of length 9 characters, and
you've told bash to iterate once, using this string as the contents of
variable z.

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

See  and
.



Re: Is this a bug by any chance?

2019-10-05 Thread
On Sat, 5 Oct 2019 18:48:35 + (UTC)
George R Goffe via Bug reports for the GNU Bourne Again SHell 
 wrote:

> Hi,
> 
> I just built the latest bash in an effort to determine if thie script shows a 
> bug or a ufu. Can you help me please?
> 
> I was expecting to see:
> 12345
> 
> Best regards AND thanks for your help,
> 
> George...
> 
> #!./bash -xv
>  x="1 2 3 4 5"
> + x='1 2 3 4 5'
>  for z in "$x"
>     do
>    echo "$z"
>     done
> + for z in "$x"
> + echo '1 2 3 4 5'
>  1 2 3 4 5
>  exit 0
> + exit 0
> 
> 

It's definitely not a bug. Quoting the expansion inhibits word splitting. Your 
loop iterates once over a single word. Said word is conveyed to echo as a 
single argument. In this case, said argument is printed by echo, with the 
addition of a newline character. If your ultimate intent is to store an 
arbitrary list of strings in a variable prior to iterating over them, you 
should use an array variable instead. As such, this would be a topic better 
suited to the help-bash list.

-- 
Kerin Millar



Is this a bug by any chance?

2019-10-05 Thread George R Goffe via Bug reports for the GNU Bourne Again SHell
Hi,

I just built the latest bash in an effort to determine if thie script shows a 
bug or a ufu. Can you help me please?

I was expecting to see:
12345

Best regards AND thanks for your help,

George...

#!./bash -xv
 x="1 2 3 4 5"
+ x='1 2 3 4 5'
 for z in "$x"
    do
   echo "$z"
    done
+ for z in "$x"
+ echo '1 2 3 4 5'
 1 2 3 4 5
 exit 0
+ exit 0