If you’ve got a string of items in bash which are delimited by a common character (comma, space, tab, etc) you can split that into an array quite easily. Simply (re)define the IFS variable to the delimiter character and assign the values to a new variable using the array=($<string_var>) syntax. The new variable will now be an array of strings as split by the delimiter character.

Let’s look at an example which converts the animal string, delimited by the “|” string, into an array before printing them all out. Notice that IFS is saved in OIFS at the start and restored at the end, just in case another script was using that variable too. Not necessary, but it’s good practice.




for ((i=0; i<${#animalArray[@]}; ++i)); do     echo "animal $i: ${animalArray[$i]}"; done


This will print:

animal 0: dog
animal 1: cat
animal 2: fish
animal 3: squirrel
animal 4: bird
animal 5: shark

  1. Scott says:

    NICE! thank you for this. This was perfect for sending mysql queries row results using CONCAT_WS to an array and parsing them.

  2. […] Convert a delimited string into an array in Bash […]

  3. Sobeer Cheema says:

    Thanks Tim, This is really helpful.

  4. Red Baron says:

    Awesome! Exactly what I was looking for.

  5. Fabio says:

    Perfect, thanks.

  6. emmm... says:

    Some quick rewrite …

    for i in “${!arr[@]}”; do
    echo animal $i: “${arr[$i]}”
    unset IFS


  7. Pavlin Georgiev says:

    Thank you for this article. It has helped me to solve a task in my bash script.

  8. […] Convert a delimited string into an array in Bash | timmurphy.org […]

  9. Putra Agung says:

    Thanks you so much