by @codeexamples

How to split a string in bash by delimiter

Home » bash » How to split a string in bash by delimiter

We can do this by setting an internal file separator (IFS) variable to delimiter ; then parse it as an array and use for loop to get the each string.

In this below example, we are splitting a dummy variable string by semicolon ;.

dummy="king;ring;ping"

# turns string to array
IFS= ";" read -ra ADDR <<< "$dummy"

# for loop
for i in "${ADDR[@]}"; do
    echo $i
done

Output:

king
ring
ping

Split string by comma

dummy="king,ring,ping"

# turns string to array
IFS= "," read -ra ADDR <<< "$dummy"

# for loop
for i in "${ADDR[@]}"; do
    echo $i
done