1

Easiest to explain with some code:

body=''
NEWLINE=$'\n'

for name in $names # don't worry about the names array
do
    body+="$name ${NEWLINE}"
done

The output is something like this:

Joe Michael Tony Peter 

Why isn't it like this?

Joe 
Michael 
Tony 
Peter 

This should work, as the newline is wrapped in single quotes, but it is ignored.

Any ideas?

Thanks

Stiofán
  • 453
  • 5
  • 17
  • 2
    You don't show enough code to reproduce. How do you generate the output? Unquoted, perhaps? – Benjamin W. Nov 20 '21 at 16:13
  • @BenjaminW. Ah! You are right. It is the echo which is messing things up, not how I am generating the string. Thanks. I'll add an answer for others in case they come across this while Googling. – Stiofán Nov 20 '21 at 16:17
  • `names` might be an array, but `$names` is equivalent to `${names[0]}` in that case. Are you sure you actually *have* an array, or just a newline-delimited string? – chepner Nov 20 '21 at 16:22

1 Answers1

1

Thanks to @Benjamin W. for pointing me in the right direction.

My issue isn't how I generate the string (that's correct), it's how I'm outputting the string.

This won't work:

echo $body

But this will:

echo "$body"
Stiofán
  • 453
  • 5
  • 17