#!/bin/bash
myvar="string 1,string 2,string 3"
# Here comma is our delimiter value
IFS="," read -a myarray <<< $myvar
echo "My array: ${myarray[@]}"
echo "My array [0]:" ${myarray[0]}
echo "Number of elements in the array: ${#myarray[@]}"
for (( i=0; i<=${#myarray[@]}; i++ )); do
echo "${myarray[$i]}"
done
When I execute the script above, Bash on Mac OS outputs it like this...
i5-Mac-mini:~ macmini$ ./testarray.sh
My array: string 1 string 2 string 3
My array [0]: string 1 string 2 string 3
Number of elements in the array: 1
string 1 string 2 string 3
String 1, String 2, and string three are all on the same line. I want them on separate lines like it does with Bash in Ubuntu Linux.
fflintstone@bedrock:~$ ./testarray.sh
My array: string 1 string 2 string 3
My array [0]: string 1
Number of elements in the array: 3
string 1
string 2
string 3
Whats going on?
I've tried inserting /n, /r, /c at the end of the echo. I've tried printf with no luck. It seems to me it has something to do with my environment setup.