Score:-1

Echo output with variable behaving differently

us flag

I have the two files below: names.csv

Firstname,Surname
Silas ,Strong
Arturo Zi,Zimmerman

and the test.sh

#!/bin/bash

while IFS=, read -r field1 field2
do
    name=$field1.$field2
    echo "Here is the name $name"
    echo "$name is included in the file"
done < names.csv

When test.sh is ran.. I get the following output where the name is missing on the second echo output.

Here is the name Firstname.Surname
 is included in the file
Here is the name Silas .Strong
 is included in the file
Here is the name Arturo Zi.Zimmerman
 is included in the file

How do i get the following output to be returned instead:

Here is the name Firstname.Surname
Firstname.Surname is included in the file
Here is the name Silas .Strong
Silas .Strong is included in the file
Here is the name Arturo Zi.Zimmerman
Arturo Zi.Zimmerman is included in the file

Thanks

hr flag
Likely your CSV file has DOS-style (CRLF) line endings
FedKad avatar
cn flag
Are you sure that you typed the second `$name` correctly in your script? In my system your script produced the expected output. However, you can run `dos2unix names.csv` before running your script to see that the previous comment is correct.
Emma avatar
us flag
@steeldriver that was the issue.. my CSV had the CRLF format. Converting it to LF fixed the file Thanks
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.