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]"
I don’t necessarily need the ADDR1
and 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="[email protected];[email protected]"
mails=$(echo $IN | tr ";" "n")
for addr in $mails
do
echo "> [$addr]"
done
Output:
> [[email protected]]
> [[email protected]]
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?
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=';'
mails2=$IN
for x in $mails2
do
echo "> [$x]"
done
IFS=$OIFS
BTW, when I tried
mails2=($IN)
I only got the first string when printing it in loop, without brackets around $IN
it works.
32 Answers
You can set the internal field separator (IFS) variable, and then let it parse into an array. When this happens in a command, then the assignment to IFS
only takes place to that single command’s environment (to read
). It then parses the input according to the IFS
variable value into an array, which we can then iterate over.
IFS=';' read -ra ADDR <<< "$IN"
for i in "${ADDR[@]}"; do
# process "$i"
done
It will parse one line of items separated by ;
, pushing it into an array. Stuff for processing whole of $IN
, each time one line of input separated by ;
:
while IFS=';' read -ra ADDR; do
for i in "${ADDR[@]}"; do
# process "$i"
done
done <<< "$IN"
Taken from Bash shell script split array:
IN="[email protected];[email protected]"
arrIN=(${IN//;/ })
Explanation:
This construction replaces all occurrences of ';'
(the initial //
means global replace) in the string IN
with ' '
(a single space), then interprets the space-delimited string as an array (that’s what the surrounding parentheses do).
The syntax used inside of the curly braces to replace each ';'
character with a ' '
character is called Parameter Expansion.
There are some common gotchas:
If you don’t mind processing them immediately, I like to do this:
for i in $(echo $IN | tr ";" "n")
do
# process
done
You could use this kind of loop to initialize an array, but there’s probably an easier way to do it. Hope this helps, though.
Compatible answer
There are a lot of different ways to do this in bash.
However, it’s important to first note that bash
has many special features (so-called bashisms) that won’t work in any other shell.
In particular, arrays, associative arrays, and pattern substitution, which are used in the solutions in this post as well as others in the thread, are bashisms and may not work under other shells that many people use.
For instance: on my Debian GNU/Linux, there is a standard shell called dash; I know many people who like to use another shell called ksh; and there is also a special tool called busybox with his own shell interpreter (ash).
Requested string
The string to be split in the above question is:
IN="[email protected];[email protected]"
I will use a modified version of this string to ensure that my solution is robust to strings containing whitespace, which could break other solutions:
IN="[email protected];[email protected];Full Name <[email protected]>"
Split string based on delimiter in bash (version >=4.2)
In pure bash
, we can create an array with elements split by a temporary value for IFS (the input field separator). The IFS, among other things, tells bash
which character(s) it should treat as a delimiter between elements when defining an array:
IN="[email protected];[email protected];Full Name <[email protected]>"
# save original IFS value so we can restore it later
oIFS="$IFS"
IFS=";"
declare -a fields=($IN)
IFS="$oIFS"
unset oIFS
In newer versions of bash
, prefixing a command with an IFS definition changes the IFS for that command only and resets it to the previous value immediately afterwards. This means we can do the above in just one line:
IFS=; read -a fields <<<"$IN"
# after this command, the IFS resets back to its previous value (here, the default):
set | grep ^IFS=
# IFS=$' tn'
We can see that the string IN
has been stored into an array named fields
, split on the semicolons:
set | grep ^fields=\|^IN=
# fields=([0]="[email protected]" [1]="[email protected]" [2]="Full Name <[email protected]>")
# IN='[email protected];[email protected];Full Name <[email protected]>'
(We can also display the contents of these variables using declare -p
🙂
declare -p IN fields
# declare -- IN="[email protected];[email protected];Full Name <[email protected]>"
# declare -a fields=([0]="[email protected]" [1]="[email protected]" [2]="Full Name <[email protected]>")
Note that read
is the quickest way to do the split because there are no forks or external resources called.
Once the array is defined, you can use a simple loop to process each field (or, rather, each element in the array you’ve now defined):
# `"${fields[@]}"` expands to return every element of `fields` array as a separate argument
for x in "${fields[@]}" ;do
echo "> [$x]"
done
# > [[email protected]]
# > [[email protected]]
# > [Full Name <[email protected]>]
Or you could drop each field from the array after processing using a shifting approach, which I like:
while [ "$fields" ] ;do
echo "> [$fields]"
# slice the array
fields=("${fields[@]:1}")
done
# > [[email protected]]
# > [[email protected]]
# > [Full Name <[email protected]>]
And if you just want a simple printout of the array, you don’t even need to loop over it:
printf "> [%s]n" "${fields[@]}"
# > [[email protected]]
# > [[email protected]]
# > [Full Name <[email protected]>]
Update: recent bash >= 4.4
In newer versions of bash
, you can also play with the command mapfile
:
mapfile -td ; fields < <(printf "%s " "$IN")
This syntax preserve special chars, newlines and empty fields!
If you don’t want to include empty fields, you could do the following:
mapfile -td ; fields <<<"$IN"
fields=("${fields[@]%$'n'}") # drop 'n' added by '<<<'
With mapfile
, you can also skip declaring an array and implicitly “loop” over the delimited elements, calling a function on each:
myPubliMail() {
printf "Seq: %6d: Sending mail to '%s'..." $1 "$2"
# mail -s "This is not a spam..." "$2" </path/to/body
printf "e[3D, done.n"
}
mapfile < <(printf "%s " "$IN") -td ; -c 1 -C myPubliMail
(Note: the