I have a bash script and I want to create an array from the lines of a txt file without the end of line character.
The file contains countries like this (may contain spaces too).
Afghanistan
Albania
Antigua and Barbuda
Argentina
Armenia
Australia
Central African Republic
.
.
.
My goal is to have an array named COUNTRIES
that holds every line from the text file above and to echo a string that has a couple of variables and the country in the middle without the end of line.
For example, for : NAME = "ALEX"
, AGE = 21
and COUNTRY=${COUNTRIES[2]}
, I want the command
echo $NAME $COUNTRY $AGE
to output ALEX Antigua and Barbuda 21
in a single line.
I tried using mapfile COUNTRIES < ${COUNTRIES_FILE}
, but the above echo outputs 21ALEX Antigua and Barbuda
for some reason.
Using mapfile -t COUNTRIES < ${COUNTRIES_FILE}
outputs the same string.
I then tried COUNTRIES+=($(cat ${COUNTRIES_FILE}))
, but that did not work either.
Any ideas?