bug-bash
[Top][All Lists]
Advanced

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

Re: Bug concatenating member of large array with string


From: Greg Wooledge
Subject: Re: Bug concatenating member of large array with string
Date: Tue, 10 Aug 2021 10:03:49 -0400

On Tue, Aug 10, 2021 at 02:26:56PM +0100, Joe Pater wrote:
> The attached bash script (named 'test'), when run on my laptop,
> produces the following output:
> 
> xyzerg
> 
> Instead of the expected:
> 
> Aabergxyz

OK, let's see if we can reproduce that.  My initial thought is carriage
return poisoning, but your attached "file" doesn't have CRs.  Then again,
that could be an artifact of an email system.

> #!/bin/bash
> 
> words=(`cat file`)
> echo ${words[0]}xyz

You really should be using mapfile for this, and quoting properly.

#!/bin/bash
mapfile -t words < file
echo "${words[0]}xyz"

> Aaberg
> Aachen
> aahing
[...]

With the script as I wrote it above, and those three lines in the input
file, I get:

unicorn:~$ ./foo
Aabergxyz

If I edit the file and put carriage returns at the end of each line,
I get:

unicorn:~$ ./foo
xyzerg

This matches your result.  So, it would appear you have carriage returns
in your input file.



reply via email to

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