How to split string based on delimiter in bash?
I have this string stored in a variable:
IN="[email protected];[email protected]"
Now I would like to split the strings by ';' delimiter so that I have
ADDR1="[email protected]"
ADDR2="[email protected]"
Don't necessarily need ADDR1
, ADDR2
variables, if they are elements of an array that's even better.
Edit: After suggestions from answers below I ended up with the following which is what I was after:
#!/usr/bin/env bash
IN="[email protected];[email protected]"
arr=$(echo $IN | tr ";" "\n")
for x in $arr
do
echo "> [$x]"
done
output:
> [[email protected]]
> [[email protected]]
Edit2: There was a solution involving setting IFS to ';', not sure what happened with that answer, how do you reset IFS back to default?
Edit3: RE: IFS solution, I tried this and it works, I keep the old IFS and then restore it:
IN="[email protected];[email protected]"
OIFS=$IFS
IFS=';'
arr2=$IN
for x in $arr2
do
echo "> [$x]"
done
IFS=$OIFS
Btw, when I tried
arr2=($IN)
I only got the first string when printing it in loop, without brackets around $IN
it works.