How to separate a statement based on some delimiter and store each field in a variable?

Hi,

Variable1 = MKT1,MKT2,MKT3,MKT4

Now i want to store each of these value seperated by comma to a array and access each of the values. Also find out number of such values seperated by comma.

Variable1 can have any number of values seperated by comma.

Thanks :slight_smile:

In Bash

Variable1=MKT1,MKT2,MKT3,MKT4 
IFS=","
declare -a Arr=($Variable1);
echo ${#Arr[@]} #Number of elements in the array
echo ${#Arr}

Hi anbu23

thanks for the reply. I tried this code as an example beore applying the same logic in my work. It worked fine. :slight_smile:
But when i incorporated the same logic in my work, it is throwing syntax error saying that unexpected (. it will not throw me error if i either remove () from declare -a Arr=($Variable1); or if i delete this line.
I cannot share my code due to privilege issues. Kindly guide me how to trouble shoot this issue.

The code snippet by anbu23 is working fine. I guess that sth in the context (some lines up/down) has been messed up. Please post those lines, eventually hiding/modifying classified portions.