Split string into multiple variables in Bash [duplicate]

Try this:

mystring="192.168.1.1,UPDOWN,Line protocol on Interface GigabitEthernet1/0/13, changed state to up"

IFS=',' read -a myarray <<< "$mystring"

echo "IP: ${myarray[0]}"
echo "STATUS: ${myarray[3]}"

In this script ${myarray[0]} refers to the first field in the comma-separated string, ${myarray[1]} refers to the second field in the comma-separated string, etc.

Leave a Comment