Thursday, February 25, 2010

bash: split string to array

There are few possible ways of splitting a string with delimiter-separated values to an array in Bash. Here I present five of them.

1. Using tr command

Output:> [123]
> [456]
> [567]
> [5]
> [343]
This method will produce incorrect array due to space in "567 5", but it works fine if there are no spaces in the $STR variable.

2. Using IFS (Internal Field Separator) variable

Output:> [123]
> [456]
> [567 5]
> [343]
This method works fine even if there are spaces.

3. Using read command

Output:> [123]
> [456]
> [567 5]
> [343]
This method also works fine even though there are spaces in the $STR variable.

4. Using sed command

Output:> [123]
> [456]
> [567]
> [5]
> [343]
This method will also produce incorrect array due to space in "567 5", but it works fine if there are no spaces in the $STR variable.

5. Using set command

Output:> [123]
> [456]
> [567 5]
> [343]
This method also works fine.


This post was mainly inspired by this StackOverflow question and my need to perform string 2 array conversions in bash.


  1. Anonymous11:07 PM

    Thanks a lot for this post.

  2. Anonymous2:31 AM

    Asking questions are actually fastidious thing if you are
    not understanding anything totally, however this article presents good understanding even.

    Also visit my web blog ... what is a graduate certificate
    My website - graduate certificate

  3. Anonymous11:47 PM

    Hi eхceptional ωеbsіtе!
    Dοes running a blog liκe this take a lot οf ωοгκ?
    Ι've absolutely no knowledge of programming but I was hoping to start my own blog soon. Anyway, if you have any suggestions or techniques for new blog owners please share. I know this is off subject nevertheless I just wanted to ask. Thanks!

    My site raspberry ketone uk

  4. Anonymous7:00 AM

    I used to be able to find good information from your blog

    Stop by my web-site; he said

  5. Anonymous12:59 PM

    I'll right away seize your rss as I can't to find your
    email subscription hyperlink or newsletter service.
    Do you've any? Kindly permit me know in order that I may subscribe. Thanks.

    Look into my page -

  6. Anonymous10:26 PM

    I'm impressed, I have to admit. Rarely do I encounter a blog that's both еquаlly educatіve and amusing, and without а doubt, yοu have hit the nаіl on the heaԁ.
    The problem iѕ an iѕsue that not
    enough peoрle are speaking intelligently about.
    Nоw i'm very happy that I came across this in my hunt for something relating to this.

    Also visit my site ... debt reduction services

  7. Anonymous6:32 PM

    This is very fascinating, You are an overly professional blogger.
    I've joined your feed and stay up for looking for more of your wonderful post. Additionally, I have shared your site in my social networks

    Also visit my webpage :: download

  8. Anonymous6:33 PM

    I'm not sure where you're getting your info, but good topic.

    I needs to spend some time learning much more or understanding more.

    Thanks for great info I was looking for this
    info for my mission.

    my web site :: download now

  9. Anonymous8:57 PM

    This is my first time go to see at here and i am in
    fact pleassant to read everthing at single place.

    Here is my web site; Garcinia cambogis review

  10. Anonymous9:39 AM

    Hey very nice site!! Man .. Excellent .. Superb .
    . I will bookmark your website and take the feeds also?
    I'm happy to seek out so many useful info right here within the publish, we'd like work out extra
    strategies in this regard, thanks for sharing. . . . .

    Stop by my page ... Nuvocleanse

  11. Anonymous8:14 PM

    First off I want to say superb blog! I had a quick question that I'd like to ask if you don't mind.
    I was curious to know how you center yourself and clear your head prior to writing.
    I have had difficulty clearing my thoughts in getting my thoughts out there.
    I truly do enjoy writing however it just seems like the first 10 to 15 minutes tend to be
    wasted simply just trying to figure out how to begin.
    Any recommendations or hints? Thank you!

    My web blog: Mito slim reviews

  12. Anonymous1:53 AM

    I'm curious to find out what blog system you have been using? I'm experiencing some small
    security problems with my latest site and I'd like to find something more safeguarded. Do you have any recommendations?

    Feel free to surf to my blog post: Work from home

  13. Anonymous3:48 AM

    I think that what you said was very logical. But, think on this,
    what if you were to create a killer headline? I ain't suggesting your content is not solid., however what if you added a title that makes people want more? I mean "bash: split string to array" is kinda plain. You could peek at Yahoo's home page
    and note how they create news headlines to grab viewers to click.
    You might add a related video or a picture or two to get
    readers excited about everything've got to say. Just my opinion, it could bring your posts a little bit more interesting.

    Also visit my blog Lipo slender reviews

  14. Anonymous12:13 PM

    Can I just say what a comfort to discover someone that really knows what they are talking about on the net.
    You definitely understand how to bring a problem
    to light and make it important. More and more people have to read this and understand
    this side of your story. I was surprised you are not more popular given
    that you surely have the gift.

    My homepage ...

  15. Anonymous5:05 AM

    Hi, i feel that i saw you visited my weblog so
    i came to return the desire?.I'm attempting to in finding issues to improve my website!I suppose its good enough to use a few of your ideas!!

    My web-site ...

  16. Anonymous12:27 PM

    Blendanomics 315: The very last Processing Leading man Have a tremor!

    In a dress and bag, that's needed simply which the Vainqueur 2000+ Tv ad Juicer. Therefore let's examine all of us established that you may
    get more cider and as a consequence get great prices
    belonging to the Hurom HU 90 long juicer. Make a decision
    on exceptional juice extractors and additionally experiment with instructions is motivated to quite simply utilize fresh veggies and fruits.
    Working with armature might be absence of feeling wracking, and eliminate your emotions if you don't find what you're looking for, as well won't chose the surface to positively guidebook for your article with right sorts.

    Review my site; kitchenaid blender parts ksb50b

  17. Anonymous12:07 AM

    By knowing that you're going to fundamentally by utilizing the juice machine to help make fluid, following that of these extra features indicates excess debts that aren't called for for all.
    often the moves so fast fresh foods do voyage back
    out from the chute. These products blenders are made now with one or two highlights comparable to Algorithm a lot more than load up defender,
    Liquidizing to Chutney, Much more Metal Containers: Dried Versus wet Weird sounds, 3rd Speeds
    a circular turns making use of Inc-her to do temporary method.

    Itrrrs majority of these which unfortunately ascertain the level of
    many advantages continues made in the direction of drink via the benefits also
    known as green vegitables preferred.

    Here is my page - cheapest blenders

  18. Anonymous4:06 PM

    You'll have a a lot more probability gaining very best Vitamix blender or food processor buys recognized designer the extender into your close location, with a purpose to spending money on supply service fees to property taxes generally if machine is distributed under the state. The main masticating juice extractor creates anyone fishing gear in strategy. The constituents on this juices put ten leading scaled top notch carrots as well as a bulky dimensions of not stale green beans. Electrical energy their particular health benefits, it's really a sturdy task
    for a person that is truly with no fantastic undercooked plus organic
    nutrients a consequence of cleansing answer.
    , the position feels alot more as well as arranged without the
    presense of wires.

    Have a look at my weblog; ratings on blenders hot soup

  19. Anonymous4:47 PM

    Most are poured in the morning utilizing butter
    in addition syrup, as biscuit complete with fruit and
    even goodies. Simply because these noodles
    appeared to be dried out, consumers reserved nicely, and thus
    turned out to be straightforward to transporting.
    As well as the colour themes adding to it has been modest think
    about the appearance of a person's kettle also toaster. More commonly, it's tough children and kids by sitting at dawn having a full-course
    breakfast. It is quite very simple to comb any cooker along with a portion of it is utilized with
    usually the garbage disposal, microwave. Residence closed while grubby, space
    at standstill.

    Visit my webpage - cream toaster 2 slice ()

  20. Anonymous10:31 PM

    Hello, just wanted to say, I loved this post. It was practical.
    Keep on posting!

    My websige ... cost of roofing a house with 20 squares

  21. Thanks! Very helpful :)

  22. Could you also please add the code to split string with delimiter containing multiple characters, for example "*+" in hello*+bash*+shell.
    I have found many examples to split using a single char as delimiter, like Bash Split String