I have this string stored in a variable:
Now I would like to split the strings by
; delimiter so that I have:
I don't necessarily need the
ADDR2 variables. If they are elements of an array that's even better.
After suggestions from the answers below, I ended up with the following which is what I was after:
#!/usr/bin/env bash IN="firstname.lastname@example.org;email@example.com" mails=$(echo $IN | tr ";" "\n") for addr in $mails do echo "> [$addr]" done
> [firstname.lastname@example.org] > [email@example.com]
There was a solution involving setting Internal_field_separator (IFS) to
;. I am not sure what happened with that answer, how do you reset
IFS back to default?
IFS solution, I tried this and it works, I keep the old
IFS and then restore it:
IN="firstname.lastname@example.org;email@example.com" OIFS=$IFS IFS=';' mails2=$IN for x in $mails2 do echo "> [$x]" done IFS=$OIFS
BTW, when I tried
I only got the first string when printing it in loop, without brackets around
$IN it works.