'Bash output different when in the console compared to when in an array

I have the following csv file mydata.csv

Afghanistan,Incidence,2018,29
Viet Nam,Incidence,2017,27
United States of America,Incidence,2016,26
United Kingdom,Incidence,2015,15

I want to extract the countries, which is easy by using cut, giving me the following output in the console:

$ cut -d "," -f 1 mydata.csv

Afghanistan  
Viet Nam  
United States of America  
United Kingdom

However, when storing it inside an array: myArray=($(cut -d , -f 1 mydata.csv))

Here is what I get:

$ echo "${myArray[@]}"  
Afghanistan Viet Nam United States of America United Kingdom

Calling echo "${myArray[3]}" gives me "United" instead of "United States of America".

So instead I have tried to use awk to add quotes around each country and store them inside an array.

$ awk '{print $0}' mydata.csv | cut -d , -f1 | awk -v q="'" '{print q $0 q}'

Gives me the following inside the console:

'Afghanistan'  
'Viet Nam'   
'United States of America'  
'United Kingdom'  

But again, when I store it inside an array using this line of code:

myArray=($(awk '{print $0}' mydata.csv | grep -v Location | cut -d , -f1 | awk -v q="'" '{print q $0 q}'))

I get the following broken up array once again:

$echo "${myArray[2]}"
Nam'

I have no idea what to do to get 'Viet Nam' (instead of Nam') as a single element or 'United States of America' as well.
Why are the outputs correct in the console and weirded up when in an array?



Solution 1:[1]

If you want to read lines into an array, word-splitting is not particularly useful. You need to either use readarray (aka mapfile) or a while read loop. If you want to take data from a command (e.g. cut) rather than a file, you need to combine that with process substitution:

readarray -t myArray < <(cut -d "," -f 1 mydata.csv)

Note that both readarray and process substitution are nonstandard bash features, so you need to run your script with bash. Use a bash shebang (#!/bin/bash or #!/usr/bin/env bash), and don't override that by running the script with the sh command.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gordon Davisson