By stefanB


2009-05-28 02:03:43 8 Comments

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.

30 comments

@F. Hauri 2013-04-13 14:20:09

Compatible answer

There are a lot of different ways to do this in .

However, it's important to first note that bash has many special features (so-called bashisms) that won't work in any other .

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 ; I know many people who like to use another shell called ; and there is also a special tool called with his own shell interpreter ().

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 (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=$' \t\n'

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 >= 4.4

In newer versions of bash, you can also play with the command mapfile:

mapfile -td \; fields < <(printf "%s\0" "$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\0" "$IN") -td \; -c 1 -C myPubliMail

(Note: the \0 at end of the format string is useless if you don't care about empty fields at end of the string or they're not present.)

mapfile < <(echo -n "$IN") -td \; -c 1 -C myPubliMail

# Seq:      0: Sending mail to '[email protected]', done.
# Seq:      1: Sending mail to '[email protected]', done.
# Seq:      2: Sending mail to 'Full Name <[email protected]>', done.

Or you could use <<<, and in the function body include some processing to drop the newline it adds:

myPubliMail() {
    local seq=$1 dest="${2%$'\n'}"
    printf "Seq: %6d: Sending mail to '%s'..." $seq "$dest"
    # mail -s "This is not a spam..." "$dest" </path/to/body
    printf "\e[3D, done.\n"
}

mapfile <<<"$IN" -td \; -c 1 -C myPubliMail

# Renders the same output:
# Seq:      0: Sending mail to '[email protected]', done.
# Seq:      1: Sending mail to '[email protected]', done.
# Seq:      2: Sending mail to 'Full Name <[email protected]>', done.

Split string based on delimiter in

If you can't use bash, or if you want to write something that can be used in many different shells, you often can't use bashisms -- and this includes the arrays we've been using in the solutions above.

However, we don't need to use arrays to loop over "elements" of a string. There is a syntax used in many shells for deleting substrings of a string from the first or last occurrence of a pattern. Note that * is a wildcard that stands for zero or more characters:

(The lack of this approach in any solution posted so far is the main reason I'm writing this answer ;)

${var#*SubStr}  # drops substring from start of string up to first occurrence of `SubStr`
${var##*SubStr} # drops substring from start of string up to last occurrence of `SubStr`
${var%SubStr*}  # drops substring from last occurrence of `SubStr` to end of string
${var%%SubStr*} # drops substring from first occurrence of `SubStr` to end of string

As explained by Score_Under:

# and % delete the shortest possible matching substring from the start and end of the string respectively, and

## and %% delete the longest possible matching substring.

Using the above syntax, we can create an approach where we extract substring "elements" from the string by deleting the substrings up to or after the delimiter.

The codeblock below works well in (including Mac OS's bash), , , and 's :

IN="[email protected];[email protected];Full Name <[email protected]>"
while [ "$IN" ] ;do
    # extract the substring from start of string up to delimiter.
    # this is the first "element" of the string.
    iter=${IN%%;*}
    echo "> [$iter]"
    # if there's only one element left, set `IN` to an empty string.
    # this causes us to exit this `while` loop.
    # else, we delete the first "element" of the string from IN, and move onto the next.
    [ "$IN" = "$iter" ] && \
        IN='' || \
        IN="${IN#*;}"
  done
# > [[email protected]]
# > [[email protected]]
# > [Full Name <[email protected]>]

Have fun!

@Score_Under 2015-04-28 16:58:33

The #, ##, %, and %% substitutions have what is IMO an easier explanation to remember (for how much they delete): # and % delete the shortest possible matching string, and ## and %% delete the longest possible.

@Isaac 2016-10-26 04:36:52

The IFS=\; read -a fields <<<"$var" fails on newlines and add a trailing newline. The other solution removes a trailing empty field.

@sancho.s ReinstateMonicaCellio 2018-10-04 03:42:55

Could the last alternative be used with a list of field separators set somewhere else? For instance, I mean to use this as a shell script, and pass a list of field separators as a positional parameter.

@F. Hauri 2018-10-04 07:47:13

Yes, in a loop: for sep in "#" "ł" "@" ; do ... var="${var#*$sep}" ...

@tylerl 2020-09-04 18:41:34

This answer is pretty epic.

@Tong 2013-01-14 06:33:40

I think AWK is the best and efficient command to resolve your problem. AWK is included by default in almost every Linux distribution.

echo "[email protected];[email protected]" | awk -F';' '{print $1,$2}'

will give

[email protected] [email protected]

Of course your can store each email address by redefining the awk print field.

@Jaro 2014-01-07 21:30:24

Or even simpler: echo "[email protected];[email protected]" | awk 'BEGIN{RS=";"} {print}'

@Aquarelle 2014-05-06 21:58:29

@Jaro This worked perfectly for me when I had a string with commas and needed to reformat it into lines. Thanks.

@Eduardo Lucio 2015-08-05 12:59:21

It worked in this scenario -> "echo "$SPLIT_0" | awk -F' inode=' '{print $1}'"! I had problems when trying to use atrings (" inode=") instead of characters (";"). $ 1, $ 2, $ 3, $ 4 are set as positions in an array! If there is a way of setting an array... better! Thanks!

@Tong 2015-08-06 02:42:02

@EduardoLucio, what I'm thinking about is maybe you can first replace your delimiter inode= into ; for example by sed -i 's/inode\=/\;/g' your_file_to_process, then define -F';' when apply awk, hope that can help you.

@Fil 2018-06-13 15:58:57

Yet another late answer... If you are java minded, here is the bashj (https://sourceforge.net/projects/bashj/) solution:

#!/usr/bin/bashj

#!java

private static String[] cuts;
private static int cnt=0;
public static void split(String words,String regexp) {cuts=words.split(regexp);}
public static String next() {return(cnt<cuts.length ? cuts[cnt++] : "null");}

#!bash

IN="[email protected];[email protected]"

: j.split($IN,";")    # java method call

while true
do
    NAME=j.next()     # java method call
    if [ $NAME != null ] ; then echo $NAME ; else exit ; fi
done

@shuaihanhungry 2018-01-20 15:54:29

you can apply awk to many situations

echo "[email protected];[email protected]"|awk -F';' '{printf "%s\n%s\n", $1, $2}'

also you can use this

echo "[email protected];[email protected]"|awk -F';' '{print $1,$2}' OFS="\n"

@Halle Knast 2017-05-24 08:42:53

The following Bash/zsh function splits its first argument on the delimiter given by the second argument:

split() {
    local string="$1"
    local delimiter="$2"
    if [ -n "$string" ]; then
        local part
        while read -d "$delimiter" part; do
            echo $part
        done <<< "$string"
        echo $part
    fi
}

For instance, the command

$ split 'a;b;c' ';'

yields

a
b
c

This output may, for instance, be piped to other commands. Example:

$ split 'a;b;c' ';' | cat -n
1   a
2   b
3   c

Compared to the other solutions given, this one has the following advantages:

  • IFS is not overriden: Due to dynamic scoping of even local variables, overriding IFS over a loop causes the new value to leak into function calls performed from within the loop.

  • Arrays are not used: Reading a string into an array using read requires the flag -a in Bash and -A in zsh.

If desired, the function may be put into a script as follows:

#!/usr/bin/env bash

split() {
    # ...
}

split "[email protected]"

@madprops 2019-06-14 05:23:30

Doesn't seem to work with delimiters longer than 1 character: split=$(split "$content" "file://")

@Halle Knast 2019-06-14 18:52:55

True - from help read: -d delim continue until the first character of DELIM is read, rather than newline

@Steven Lizarazo 2016-08-11 20:45:25

This worked for me:

string="1;2"
echo $string | cut -d';' -f1 # output is 1
echo $string | cut -d';' -f2 # output is 2

@GuyPaddock 2018-12-12 01:37:50

Though it only works with a single character delimiter, that's what the OP was looking for (records delimited by a semicolon).

@MAChitgarha 2020-04-03 09:41:34

Answered about four years ago by @Ashok, and also, more than one year ago by @DougW, than your answer, with even more information. Please post different solution than others'.

@eukras 2012-10-22 07:10:09

There are some cool answers here (errator esp.), but for something analogous to split in other languages -- which is what I took the original question to mean -- I settled on this:

IN="[email protected];[email protected]"
declare -a a="(${IN/;/ })";

Now ${a[0]}, ${a[1]}, etc, are as you would expect. Use ${#a[*]} for number of terms. Or to iterate, of course:

for i in ${a[*]}; do echo $i; done

IMPORTANT NOTE:

This works in cases where there are no spaces to worry about, which solved my problem, but may not solve yours. Go with the $IFS solution(s) in that case.

@olibre 2013-10-07 13:33:38

Does not work when IN contains more than two e-mail addresses. Please refer to same idea (but fixed) at palindrom's answer

@Isaac 2016-10-26 05:14:58

Better use ${IN//;/ } (double slash) to make it also work with more than two values. Beware that any wildcard (*?[) will be expanded. And a trailing empty field will be discarded.

@kenorb 2015-09-11 20:54:49

Here is a clean 3-liner:

in="[email protected];[email protected];[email protected];[email protected]"
IFS=';' list=($in)
for item in "${list[@]}"; do echo $item; done

where IFS delimit words based on the separator and () is used to create an array. Then [@] is used to return each item as a separate word.

If you've any code after that, you also need to restore $IFS, e.g. unset IFS.

@Isaac 2016-10-26 05:03:02

The use of $in unquoted allows wildcards to be expanded.

@rashok 2016-10-25 12:41:51

IN="[email protected];[email protected]"
IFS=';'
read -a IN_arr <<< "${IN}"
for entry in "${IN_arr[@]}"
do
    echo $entry
done

Output

[email protected]
[email protected]

System : Ubuntu 12.04.1

@codeforester 2017-01-02 05:37:09

IFS is not getting set in the specific context of read here and hence it can upset rest of the code, if any.

@Emilien Brigand 2016-08-01 13:15:07

Without setting the IFS

If you just have one colon you can do that:

a="foo:bar"
b=${a%:*}
c=${a##*:}

you will get:

b = foo
c = bar

@palindrom 2011-03-10 09:00:43

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:

  1. If the original string has spaces, you will need to use IFS:
    • IFS=':'; arrIN=($IN); unset IFS;
  2. If the original string has spaces and the delimiter is a new line, you can set IFS with:
    • IFS=$'\n'; arrIN=($IN); unset IFS;

@Oz123 2011-03-21 18:50:09

I just want to add: this is the simplest of all, you can access array elements with ${arrIN[1]} (starting from zeros of course)

@KomodoDave 2012-01-05 15:13:36

Found it: the technique of modifying a variable within a ${} is known as 'parameter expansion'.

@David Parks 2012-12-01 04:21:13

If you want to split on a special character such as tilde (~) make sure to escape it: arrIN=(${IN//\~/ })

@Ethan 2013-04-12 22:47:51

No, I don't think this works when there are also spaces present... it's converting the ',' to ' ' and then building a space-separated array.

@mklement0 2013-04-24 14:08:36

Very concise, but there are caveats for general use: the shell applies word splitting and expansions to the string, which may be undesired; just try it with. IN="[email protected];[email protected];*;broken apart". In short: this approach will break, if your tokens contain embedded spaces and/or chars. such as * that happen to make a token match filenames in the current folder.

@Charles Duffy 2013-07-06 14:39:57

This is a bad approach for other reasons: For instance, if your string contains ;*;, then the * will be expanded to a list of filenames in the current directory. -1

@Kyle Strand 2015-02-18 23:37:41

You can actually fix the spaces problem by using IFS instead of parameter expansion/substitution: IFS=':' arrIN=($IN) This is also somewhat more readable in my opinion.

@Eliah Kagan 2015-05-08 00:18:26

@KyleStrand That sets IFS, then sets arrIN, same as if they were executed on separate lines or separated by a ;. That is, assignments are temporary only if they appear before a non-assignment command. So after IFS=':' arrIN=($IN), echo "$IFS" gives : and words are split on : for subsequent commands, which usually isn't wanted. (This is easy to overlook, since echo $var is sufficient to check if $var is :, when : is not in $IFS.) Therefore, except perhaps at the very end of a script, IFS=':' arrIN=($IN) IFS=$' \t\n' or IFS=':' arrIN=($IN); unset IFS is preferable.

@Kyle Strand 2015-05-08 00:50:56

@EliahKagan Ah. Is there some benefit to that particular inconsistency?

@Eliah Kagan 2015-05-08 02:42:38

@KyleStrand Yes, in that while it makes sense for a variable assignment to be scoped to a command, it doesn't really make sense for one variable assignment to be scoped to another. The shell performs variable/parameter expansion before it assigns values (or runs commands). For example, x=foo echo $x doesn't output foo, as $x is expanded before foo is assigned to x or echo is run. Likewise, if x=foo y=$x were to assign foo to x only while y=$x ran, then y would be assigned the original $x (not foo) because $x would expand before any variable assignments happened.

@John_West 2016-01-06 01:08:05

@CharlesDuffy This could be avoided with set -f: set -f; IN="[email protected].com;*;[email protected]"; arrIN=(${IN//;/ }); echo ${arrIN[1]}

@Charles Duffy 2016-01-06 04:19:29

@John_West, yes, this approach can be made usable by modifying global state to disable globbing (and taking close control of further global state in the form of IFS), but... well, why would you do that when read -a is available with none of the risks?

@Trevor Boyd Smith 2016-06-17 16:10:17

@Ethan thanks for pointing out the problem with when spaces are present and I am surprised this gotcha was not in the answer. I took the liberty of editing the answer to mention this gotcha and provide a solution for it (and one other gotcha). (@EliahKagan thanks for giving a good solution that is consistent with the original answer.)

@ghoti 2016-09-03 19:12:43

Not sure why IFS=';' declare -a arr=($IN) isn't getting more cred here. No need to set any intermediary variables, the IFS change only applies to the one declare command, and we expand on the IFS rather than having to change it to something else.

@zpon 2016-11-08 07:29:41

Shouldn't IFS=':'; be IFS=';'; to match the input string? in the later example

@Nikos Alexandris 2018-11-05 16:07:54

Why does the syntax arrIN=(${IN//;/ }) break when put inside a for loop, in a bash script?

@Luiz Fernando Lobo 2020-04-01 19:14:42

Also if you are using strings that have IFS in the process this solution is the only one that won't break the process.

@caduceus 2020-05-05 08:33:31

@CharlesDuffy because its read -A in zsh and i really don't like using build-in functions

@Charles Duffy 2020-05-05 15:30:07

You can write a wrapper around a built-in function that calls the right underlying implementation/usage, if you really want to write a script that works on two mutually-incompatible shells (I strongly prefer to just use an appropriate shebang, and then a guard at the first line that exits if invoked with an incompatible interpreter). By contrast, you can't write portability shims for syntax.

@Eduardo Lucio 2016-04-04 19:54:13

Okay guys!

Here's my answer!

DELIMITER_VAL='='

read -d '' F_ABOUT_DISTRO_R <<"EOF"
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=14.04
DISTRIB_CODENAME=trusty
DISTRIB_DESCRIPTION="Ubuntu 14.04.4 LTS"
NAME="Ubuntu"
VERSION="14.04.4 LTS, Trusty Tahr"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 14.04.4 LTS"
VERSION_ID="14.04"
HOME_URL="http://www.ubuntu.com/"
SUPPORT_URL="http://help.ubuntu.com/"
BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
EOF

SPLIT_NOW=$(awk -F$DELIMITER_VAL '{for(i=1;i<=NF;i++){printf "%s\n", $i}}' <<<"${F_ABOUT_DISTRO_R}")
while read -r line; do
   SPLIT+=("$line")
done <<< "$SPLIT_NOW"
for i in "${SPLIT[@]}"; do
    echo "$i"
done

Why this approach is "the best" for me?

Because of two reasons:

  1. You do not need to escape the delimiter;
  2. You will not have problem with blank spaces. The value will be properly separated in the array!

[]'s

@gniourf_gniourf 2017-01-30 08:26:41

FYI, /etc/os-release and /etc/lsb-release are meant to be sourced, and not parsed. So your method is really wrong. Moreover, you're not quite answering the question about spiltting a string on a delimiter.

@Petr Újezdský 2016-02-26 12:20:31

Maybe not the most elegant solution, but works with * and spaces:

IN="[email protected] me.com;*;[email protected]"
for i in `delims=${IN//[^;]}; seq 1 $((${#delims} + 1))`
do
   echo "> [`echo $IN | cut -d';' -f$i`]"
done

Outputs

> [[email protected] me.com]
> [*]
> [[email protected]]

Other example (delimiters at beginning and end):

IN=";[email protected] me.com;*;[email protected];"
> []
> [[email protected] me.com]
> [*]
> [[email protected]]
> []

Basically it removes every character other than ; making delims eg. ;;;. Then it does for loop from 1 to number-of-delimiters as counted by ${#delims}. The final step is to safely get the $ith part using cut.

@gniourf_gniourf 2014-06-26 09:11:21

In Bash, a bullet proof way, that will work even if your variable contains newlines:

IFS=';' read -d '' -ra array < <(printf '%s;\0' "$in")

Look:

$ in=$'one;two three;*;there is\na newline\nin this field'
$ IFS=';' read -d '' -ra array < <(printf '%s;\0' "$in")
$ declare -p array
declare -a array='([0]="one" [1]="two three" [2]="*" [3]="there is
a newline
in this field")'

The trick for this to work is to use the -d option of read (delimiter) with an empty delimiter, so that read is forced to read everything it's fed. And we feed read with exactly the content of the variable in, with no trailing newline thanks to printf. Note that's we're also putting the delimiter in printf to ensure that the string passed to read has a trailing delimiter. Without it, read would trim potential trailing empty fields:

$ in='one;two;three;'    # there's an empty field
$ IFS=';' read -d '' -ra array < <(printf '%s;\0' "$in")
$ declare -p array
declare -a array='([0]="one" [1]="two" [2]="three" [3]="")'

the trailing empty field is preserved.


Update for Bash≥4.4

Since Bash 4.4, the builtin mapfile (aka readarray) supports the -d option to specify a delimiter. Hence another canonical way is:

mapfile -d ';' -t array < <(printf '%s;' "$in")

@John_West 2016-01-08 12:10:43

I found it as the rare solution on that list that works correctly with \n, spaces and * simultaneously. Also, no loops; array variable is accessible in the shell after execution (contrary to the highest upvoted answer). Note, in=$'...', it does not work with double quotes. I think, it needs more upvotes.

@Victor Choy 2015-09-16 03:34:51

There is a simple and smart way like this:

echo "add:sfff" | xargs -d: -i  echo {}

But you must use gnu xargs, BSD xargs cant support -d delim. If you use apple mac like me. You can install gnu xargs :

brew install findutils

then

echo "add:sfff" | gxargs -d: -i  echo {}

@DougW 2015-04-27 18:20:34

I've seen a couple of answers referencing the cut command, but they've all been deleted. It's a little odd that nobody has elaborated on that, because I think it's one of the more useful commands for doing this type of thing, especially for parsing delimited log files.

In the case of splitting this specific example into a bash script array, tr is probably more efficient, but cut can be used, and is more effective if you want to pull specific fields from the middle.

Example:

$ echo "[email protected];[email protected]" | cut -d ";" -f 1
[email protected]
$ echo "[email protected];[email protected]" | cut -d ";" -f 2
[email protected]

You can obviously put that into a loop, and iterate the -f parameter to pull each field independently.

This gets more useful when you have a delimited log file with rows like this:

2015-04-27|12345|some action|an attribute|meta data

cut is very handy to be able to cat this file and select a particular field for further processing.

@MisterMiyagi 2016-11-02 08:42:39

Kudos for using cut, it's the right tool for the job! Much cleared than any of those shell hacks.

@uli42 2017-09-14 08:30:06

This approach will only work if you know the number of elements in advance; you'd need to program some more logic around it. It also runs an external tool for every element.

@Louis Loudog Trottier 2018-05-10 04:20:30

Excatly waht i was looking for trying to avoid empty string in a csv. Now i can point the exact 'column' value as well. Work with IFS already used in a loop. Better than expected for my situation.

@Milos Grujic 2019-10-21 09:07:43

Very useful for pulling IDs and PIDs too i.e.

@Gucu112 2020-01-03 17:26:01

This answer is worth scrolling down over half a page :)

@ajaaskel 2014-10-10 11:33:54

IN='[email protected];[email protected];Charlie Brown <[email protected];!"#$%&/()[]{}*? are no problem;simple is beautiful :-)'
set -f
oldifs="$IFS"
IFS=';'; arrayIN=($IN)
IFS="$oldifs"
for i in "${arrayIN[@]}"; do
echo "$i"
done
set +f

Output:

[email protected]
[email protected]
Charlie Brown <[email protected]
!"#$%&/()[]{}*? are no problem
simple is beautiful :-)

Explanation: Simple assignment using parenthesis () converts semicolon separated list into an array provided you have correct IFS while doing that. Standard FOR loop handles individual items in that array as usual. Notice that the list given for IN variable must be "hard" quoted, that is, with single ticks.

IFS must be saved and restored since Bash does not treat an assignment the same way as a command. An alternate workaround is to wrap the assignment inside a function and call that function with a modified IFS. In that case separate saving/restoring of IFS is not needed. Thanks for "Bize" for pointing that out.

@gniourf_gniourf 2015-02-20 16:45:28

!"#$%&/()[]{}*? are no problem well... not quite: []*? are glob characters. So what about creating this directory and file: `mkdir '!"#$%&'; touch '!"#$%&/()[]{} got you hahahaha - are no problem' and running your command? simple may be beautiful, but when it's broken, it's broken.

@ajaaskel 2015-02-25 07:20:48

@gniourf_gniourf The string is stored in a variable. Please see the original question.

@gniourf_gniourf 2015-02-25 07:26:44

@ajaaskel you didn't fully understand my comment. Go in a scratch directory and issue these commands: mkdir '!"#$%&'; touch '!"#$%&/()[]{} got you hahahaha - are no problem'. They will only create a directory and a file, with weird looking names, I must admit. Then run your commands with the exact IN you gave: IN='[email protected];[email protected];Charlie Brown <[email protected];!"#$%&/()[]{}*? are no problem;simple is beautiful :-)'. You'll see that you won't get the output you expect. Because you're using a method subject to pathname expansions to split your string.

@gniourf_gniourf 2015-02-25 07:29:48

This is to demonstrate that the characters *, ?, [...] and even, if extglob is set, !(...), @(...), ?(...), +(...) are problems with this method!

@gniourf_gniourf 2015-02-25 07:31:20

One more argument against your method for the road: if someone uses this method with nullglob or failglob set, there'll be some surprises! you can try it: run your code with shopt -s nullglob and also with shopt -s failglob.

@ajaaskel 2015-02-26 15:26:03

@gniourf_gniourf Thanks for detailed comments on globbing. I adjusted the code to have globbing off. My point was however just to show that rather simple assignment can do the splitting job.

@18446744073709551615 2015-02-20 10:49:59

In Android shell, most of the proposed methods just do not work:

$ IFS=':' read -ra ADDR <<<"$PATH"                             
/system/bin/sh: can't create temporary file /sqlite_stmt_journals/mksh.EbNoR10629: No such file or directory

What does work is:

$ for i in ${PATH//:/ }; do echo $i; done
/sbin
/vendor/bin
/system/sbin
/system/bin
/system/xbin

where // means global replacement.

@Isaac 2016-10-26 05:08:13

Fails if any part of $PATH contains spaces (or newlines). Also expands wildcards (asterisk *, question mark ? and braces […]).

@fedorqui 'SO stop harming' 2015-01-08 10:21:45

Apart from the fantastic answers that were already provided, if it is just a matter of printing out the data you may consider using awk:

awk -F";" '{for (i=1;i<=NF;i++) printf("> [%s]\n", $i)}' <<< "$IN"

This sets the field separator to ;, so that it can loop through the fields with a for loop and print accordingly.

Test

$ IN="[email protected];[email protected]"
$ awk -F";" '{for (i=1;i<=NF;i++) printf("> [%s]\n", $i)}' <<< "$IN"
> [[email protected]]
> [[email protected]]

With another input:

$ awk -F";" '{for (i=1;i<=NF;i++) printf("> [%s]\n", $i)}' <<< "a;b;c   d;e_;f"
> [a]
> [b]
> [c   d]
> [e_]
> [f]

@Michael Hale 2012-06-14 17:38:03

A one-liner to split a string separated by ';' into an array is:

IN="[email protected];[email protected]"
ADDRS=( $(IFS=";" echo "$IN") )
echo ${ADDRS[0]}
echo ${ADDRS[1]}

This only sets IFS in a subshell, so you don't have to worry about saving and restoring its value.

@Luca Borrione 2012-09-03 10:04:23

-1 this doesn't work here (ubuntu 12.04). it prints only the first echo with all $IN value in it, while the second is empty. you can see it if you put echo "0: "${ADDRS[0]}\n echo "1: "${ADDRS[1]} the output is0: [email protected];[email protected]\n 1: (\n is new line)

@Luca Borrione 2012-09-03 10:05:06

please refer to nickjb's answer at for a working alternative to this idea stackoverflow.com/a/6583589/1032370

@Score_Under 2015-04-28 17:09:39

-1, 1. IFS isn't being set in that subshell (it's being passed to the environment of "echo", which is a builtin, so nothing is happening anyway). 2. $IN is quoted so it isn't subject to IFS splitting. 3. The process substitution is split by whitespace, but this may corrupt the original data.

@Ashok 2012-09-08 05:01:42

This also works:

IN="[email protected];[email protected]"
echo ADD1=`echo $IN | cut -d \; -f 1`
echo ADD2=`echo $IN | cut -d \; -f 2`

Be careful, this solution is not always correct. In case you pass "[email protected]" only, it will assign it to both ADD1 and ADD2.

@fersarr 2016-03-03 17:17:52

You can use -s to avoid the mentioned problem: superuser.com/questions/896800/… "-f, --fields=LIST select only these fields; also print any line that contains no delimiter character, unless the -s option is specified"

@NevilleDNZ 2013-09-02 06:30:53

Two bourne-ish alternatives where neither require bash arrays:

Case 1: Keep it nice and simple: Use a NewLine as the Record-Separator... eg.

IN="[email protected]
[email protected]"

while read i; do
  # process "$i" ... eg.
    echo "[email:$i]"
done <<< "$IN"

Note: in this first case no sub-process is forked to assist with list manipulation.

Idea: Maybe it is worth using NL extensively internally, and only converting to a different RS when generating the final result externally.

Case 2: Using a ";" as a record separator... eg.

NL="
" IRS=";" ORS=";"

conv_IRS() {
  exec tr "$1" "$NL"
}

conv_ORS() {
  exec tr "$NL" "$1"
}

IN="[email protected];[email protected]"
IN="$(conv_IRS ";" <<< "$IN")"

while read i; do
  # process "$i" ... eg.
    echo -n "[email:$i]$ORS"
done <<< "$IN"

In both cases a sub-list can be composed within the loop is persistent after the loop has completed. This is useful when manipulating lists in memory, instead storing lists in files. {p.s. keep calm and carry on B-) }

@jeberle 2013-04-30 03:10:43

Use the set built-in to load up the [email protected] array:

IN="[email protected];[email protected]"
IFS=';'; set $IN; IFS=$' \t\n'

Then, let the party begin:

echo $#
for a; do echo $a; done
ADDR1=$1 ADDR2=$2

@Isaac 2016-10-26 05:17:41

Better use set -- $IN to avoid some issues with "$IN" starting with dash. Still, the unquoted expansion of $IN will expand wildcards (*?[).

@ghost 2013-04-24 13:13:57

If no space, Why not this?

IN="[email protected];[email protected]"
arr=(`echo $IN | tr ';' ' '`)

echo ${arr[0]}
echo ${arr[1]}

@ishtiyaq husain 2011-10-12 11:09:41

There are two simple methods:

cat "text1;text2;text3" | tr " " "\n"

and

cat "text1;text2;text3" | sed -e 's/ /\n/g'

@daboross 2013-06-29 20:58:03

I think you have cat and echo confused. cat reads from files. echo reads text given.

@nickjb 2011-07-05 13:41:34

A different take on Darron's answer, this is how I do it:

IN="[email protected];[email protected]"
read ADDR1 ADDR2 <<<$(IFS=";"; echo $IN)

@nickjb 2011-10-06 15:33:48

I think it does! Run the commands above and then "echo $ADDR1 ... $ADDR2" and i get "[email protected] ... [email protected]" output

@Nick 2011-10-28 14:36:47

This worked REALLY well for me... I used it to itterate over an array of strings which contained comma separated DB,SERVER,PORT data to use mysqldump.

@dubiousjim 2012-05-31 05:28:59

Diagnosis: the IFS=";" assignment exists only in the $(...; echo $IN) subshell; this is why some readers (including me) initially think it won't work. I assumed that all of $IN was getting slurped up by ADDR1. But nickjb is correct; it does work. The reason is that echo $IN command parses its arguments using the current value of $IFS, but then echoes them to stdout using a space delimiter, regardless of the setting of $IFS. So the net effect is as though one had called read ADDR1 ADDR2 <<< "[email protected] [email protected]" (note the input is space-separated not ;-separated).

@Isaac 2016-10-26 04:43:25

This fails on spaces and newlines, and also expand wildcards * in the echo $IN with an unquoted variable expansion.

@Michael Gaskill 2017-01-30 02:28:51

I really like this solution. A description of why it works would be very useful and make it a better overall answer.

@Johannes Schaub - litb 2009-05-28 02:23:27

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"

@Chris Lutz 2009-05-28 02:25:24

This is probably the best way. How long will IFS persist in it's current value, can it mess up my code by being set when it shouldn't be, and how can I reset it when I'm done with it?

@Johannes Schaub - litb 2009-05-28 03:04:10

now after the fix applied, only within the duration of the read command :)

@stefanB 2009-05-28 03:11:40

I knew there was a way with arrays, just couldn't remember what it was. I like setting the IFS but am not sure with the redirect from $IN and go through read just to populate array. Isn't just restoring IFS easier? Anyway +1 fro IFS suggestion, thanks.

@Johannes Schaub - litb 2009-05-28 03:14:27

I didn't like this saved="$IFS"; IFS=';'; ADDR=($IN); IFS="$saved" mess. :)

@lhunath 2009-05-28 06:14:17

You can read everything at once without using a while loop: read -r -d '' -a addr <<< "$in" # The -d '' is key here, it tells read not to stop at the first newline (which is the default -d) but to continue until EOF or a NULL byte (which only occur in binary data).

@Johannes Schaub - litb 2009-05-28 15:23:28

lhunath, ah nice idea :) However when i say "-d ''", then it always adds a linefeed as last element to the array. I don't know why that is :(

@Eduardo Lago Aguilar 2011-09-08 15:33:58

Seems to me the natural solution to the problem of splitting a line in bash with a custom word delimiter in a safe manner. Help me a lot.

@Luca Borrione 2012-09-03 09:23:02

+1 Only a side note: shouldn't it be recommendable to keep the old IFS and then restore it? (as shown by stefanB in his edit3) people landing here (sometimes just copying and pasting a solution) might not think about this.

@Charles Duffy 2013-07-06 14:39:04

@LucaBorrione Setting IFS on the same line as the read with no semicolon or other separator, as opposed to in a separate command, scopes it to that command -- so it's always "restored"; you don't need to do anything manually.

@imagineerThat 2014-01-09 21:20:18

I noticed that parentheses are needed around $IN. Otherwise the whole string gets put into ADDR[0]. Why is this the case?

@chepner 2014-10-02 03:50:05

@imagineerThis There is a bug involving herestrings and local changes to IFS that requires $IN to be quoted. The bug is fixed in bash 4.3.

@John_West 2016-01-08 12:03:54

Does not parse newline (\n) correctly, neither when IN declared like IN=$'...' nor when IN="...". To see it, try echo $i in for loop, or declare -p ADDR. See that solution for a workaround.

@Isaac 2016-10-26 03:28:24

Doesn't process included newlines. Also add a trailing newline.

@ssc 2016-12-07 09:19:27

This produces an extra empty array elements if the string to split by has more than one character.

@hychou 2020-05-11 12:39:28

In docker alpine (with apk add bash), this approach results in this error cannot create temp file for here-document: Text file busy

@Aaron Franke 2020-07-27 03:51:30

What about if I only need the first part?

@James 2011-09-25 01:09:38

This is the simplest way to do it.

spo='one;two;three'
OIFS=$IFS
IFS=';'
spo_array=($spo)
IFS=$OIFS
echo ${spo_array[*]}

@errator 2009-05-28 10:31:16

How about this approach:

IN="[email protected];[email protected]" 
set -- "$IN" 
IFS=";"; declare -a Array=($*) 
echo "${Array[@]}" 
echo "${Array[0]}" 
echo "${Array[1]}" 

Source

@Yzmir Ramirez 2011-09-05 01:06:06

+1 ... but I wouldn't name the variable "Array" ... pet peev I guess. Good solution.

@ata 2011-11-03 22:33:31

+1 ... but the "set" and declare -a are unnecessary. You could as well have used just IFS";" && Array=($IN)

@Luca Borrione 2012-09-03 09:26:04

+1 Only a side note: shouldn't it be recommendable to keep the old IFS and then restore it? (as shown by stefanB in his edit3) people landing here (sometimes just copying and pasting a solution) might not think about this

@Charles Duffy 2013-07-06 14:44:29

-1: First, @ata is right that most of the commands in this do nothing. Second, it uses word-splitting to form the array, and doesn't do anything to inhibit glob-expansion when doing so (so if you have glob characters in any of the array elements, those elements are replaced with matching filenames).

@John_West 2016-01-08 12:29:54

Suggest to use $'...': IN=$'[email protected];[email protected];bet <[email protected]\ns* kl.com>'. Then echo "${Array[2]}" will print a string with newline. set -- "$IN" is also neccessary in this case. Yes, to prevent glob expansion, the solution should include set -f.

@mgutt 2019-11-28 14:03:22

The external link does not explain this code. Please add an explanation for set and for $*.

@Darron 2010-09-13 20:10:42

How about this one liner, if you're not using arrays:

IFS=';' read ADDR1 ADDR2 <<<$IN

@dubiousjim 2012-05-31 05:36:47

Consider using read -r ... to ensure that, for example, the two characters "\t" in the input end up as the same two characters in your variables (instead of a single tab char).

@Luca Borrione 2012-09-03 10:07:29

-1 This is not working here (ubuntu 12.04). Adding echo "ADDR1 $ADDR1"\n echo "ADDR2 $ADDR2" to your snippet will output ADDR1 [email protected] [email protected]\nADDR2 (\n is newline)

@chepner 2015-09-19 13:59:33

This is probably due to a bug involving IFS and here strings that was fixed in bash 4.3. Quoting $IN should fix it. (In theory, $IN is not subject to word splitting or globbing after it expands, meaning the quotes should be unnecessary. Even in 4.3, though, there's at least one bug remaining--reported and scheduled to be fixed--so quoting remains a good idea.)

@Isaac 2016-10-26 04:55:59

This breaks if $in contain newlines even if $IN is quoted. And adds a trailing newline.

@Steven the Easily Amused 2019-09-01 14:36:44

A problem with this, and many other solutions is also that it assumes there are EXACTLY TWO elements in $IN - OR that you're willing to have the second and subsequent items smashed together in ADDR2. I understand that this meets the ask, but it's a time bomb.

Related Questions

Sponsored Content

64 Answered Questions

[SOLVED] How do you split a list into evenly sized chunks?

35 Answered Questions

[SOLVED] How can I check if a directory exists in a Bash shell script?

  • 2008-09-12 20:06:25
  • Grundlefleck
  • 2839604 View
  • 3806 Score
  • 35 Answer
  • Tags:   bash shell unix posix

30 Answered Questions

[SOLVED] How to concatenate string variables in Bash

37 Answered Questions

[SOLVED] How do I parse command line arguments in Bash?

67 Answered Questions

[SOLVED] How to get the source directory of a Bash script from within the script itself?

  • 2008-09-12 20:39:56
  • Jiaaro
  • 1793134 View
  • 5110 Score
  • 67 Answer
  • Tags:   bash directory

37 Answered Questions

[SOLVED] How can I check if a program exists from a Bash script?

  • 2009-02-26 21:52:49
  • gregh
  • 728699 View
  • 2307 Score
  • 37 Answer
  • Tags:   bash

79 Answered Questions

[SOLVED] How do I iterate over the words of a string?

  • 2008-10-25 08:58:21
  • Ashwin Nanjappa
  • 2198247 View
  • 3033 Score
  • 79 Answer
  • Tags:   c++ string split

20 Answered Questions

[SOLVED] How do I tell if a regular file does not exist in Bash?

  • 2009-03-12 14:48:43
  • Bill the Lizard
  • 2573643 View
  • 3339 Score
  • 20 Answer
  • Tags:   bash file-io scripting

26 Answered Questions

[SOLVED] How to check if a string contains a substring in Bash

19 Answered Questions

[SOLVED] Loop through an array of strings in Bash?

  • 2012-01-16 13:21:16
  • Mo.
  • 1289222 View
  • 1570 Score
  • 19 Answer
  • Tags:   arrays bash shell

Sponsored Content