bashecho

Strange "echo" behavior in shell script


I want to print the content that I have obtained from the split of an array in this way:

string="abc test;ABCtest.it"

IFS=';' read -a array <<< "$string"
name="${array[0]}"
url="${array[1]}"

echo -ne "\n$url,$name" >> "$outputDir/$fileName"

But the output file doesn't contain the URL part.

I think that the problem is the ., but I don't know how to fix it.

If I try this

echo $url

it works!

I've tried also with printf and with an hardcoded filename, but nothing!

printf '%s %s\n' "$url" "$name"  >> test.txt

It seems that when I try to concatenate another thing after the variable $url, some part of the variable is deleted or overwritten into the output file.

For example, if I try with this:

printf '%s %s\n' "$url" "pp"  >> test.txt

What I get with a simple cat test.txt is this:

 pptest.it

but the content of the variable $url must be ABCTest.it.


Solution

  • To complement chepner's helpful answer: