Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7942413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:57:44+00:00 2026-06-03T23:57:44+00:00

In a Bash script, I would like to split a line into pieces and

  • 0

In a Bash script, I would like to split a line into pieces and store them in an array.

For example, given the line:

Paris, France, Europe

I would like to have the resulting array to look like so:

array[0] = Paris
array[1] = France
array[2] = Europe

A simple implementation is preferable; speed does not matter. How can I do it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T23:57:46+00:00Added an answer on June 3, 2026 at 11:57 pm
    IFS=', ' read -r -a array <<< "$string"
    

    Note that the characters in $IFS are treated individually as separators so that in this case fields may be separated by either a comma or a space rather than the sequence of the two characters. Interestingly though, empty fields aren’t created when comma-space appears in the input because the space is treated specially.

    To access an individual element:

    echo "${array[0]}"
    

    To iterate over the elements:

    for element in "${array[@]}"
    do
        echo "$element"
    done
    

    To get both the index and the value:

    for index in "${!array[@]}"
    do
        echo "$index ${array[index]}"
    done
    

    The last example is useful because Bash arrays are sparse. In other words, you can delete an element or add an element and then the indices are not contiguous.

    unset "array[1]"
    array[42]=Earth
    

    To get the number of elements in an array:

    echo "${#array[@]}"
    

    As mentioned above, arrays can be sparse so you shouldn’t use the length to get the last element. Here’s how you can in Bash 4.2 and later:

    echo "${array[-1]}"
    

    in any version of Bash (from somewhere after 2.05b):

    echo "${array[@]: -1:1}"
    

    Larger negative offsets select farther from the end of the array. Note the space before the minus sign in the older form. It is required.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a bash script mystuff containing a line like lynx -dump http://example.com >tmpfile
In a Bash script, I would like to do something like: app1 & pidApp1=$!
I would like to call a Python script from within a Bash while loop.
I want to make a bash script that works like a makefile. It would
I am learning bash. I would like to do a simple script that, when
I would like to test in my bash script where stdout and stderr are
I would like to do following: I have a bash script that calls an
In a bash script I would like to parse the names of all subdirectories
I would like to write a bash script or a few shell commands that
Inside my bash script, I would like to parse zero, one or two parameters

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.