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 6174087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:41:32+00:00 2026-05-23T23:41:32+00:00

I have successfully made a join function, which joins an array to a string

  • 0

I have successfully made a join function, which joins an array to a string using the delimeter:

function join() # Usage: string=$(join "delimeter" "${array[@]}" )
{
    local array=( "${@:2}" )
    OLD_IFS="$IFS"
    IFS="$1"
    local string="${array[*]}"
    IFS="$OLD_IFS"
    echo "$string"
}

I have also tried to make a split function which should do the opposite:

function split() # Usage: array=( $(split "delimeter" "$string") )
{
    OLD_IFS="$IFS"
    IFS="$1"
    local array=( $2 )
    IFS="$OLD_IFS"
    echo "${array[@]}"
}

But, when I use the split command and the result contains spaces, it will not work as expected.
Example:

array=( "foo" "bar" "baz" "foo bar" )
string=$(join "|" "${array[@]}")
echo $string
array=( $(split "|" "$string") )
for i in ${array[@]}
do
    echo $i
done

The last element “foo bar” has been split too.
I think the solution is that you have to do array=( "$(split '|' "$string")" ), but I do not know how to nest the quotes probably.

  • 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-05-23T23:41:33+00:00Added an answer on May 23, 2026 at 11:41 pm

    Note that IFS can be a local variable in a function, so you don’t have to backup and restore its value:

    join () { local IFS="$1"; local s="${@:2}"; printf "%s" "$s"; }
    

    Here’s an implementation of split that requires an eval:

    # usage: split varName separator stringToSplit
    split () {
      local var="$1"
      local IFS="$2"
      set -- $3
      echo "declare -a $var=( $(printf "\"%s\" " "$@") )"
    }
    

    Demonstration

    $ a=( foo bar "hello world" baz )
    $ s=$(join , "${a[@]}")
    $ echo $s
    foo,bar,hello world,baz
    $ split b , "$s"
    declare -a b=( "foo" "bar" "hello world" "baz"  )
    $ eval $(split b , "$s")
    $ for x in "${b[@]}"; do echo "$x"; done
    foo
    bar
    hello world
    baz
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have successfully made my 4 CRUD actions restful by using mapResources in the
I have a UIWebView which displays some local text and images. Either I've made
using the Code Snippet for sending email in VB.Net I have successfully sent an
What I have now (which successfully loads the plug-in) is this: Assembly myDLL =
I have been using Hibernate very successfully, but today I noticed a bizarre phenomenon
I have successfully made, committed, and pushed changes to a central git repository. I
I have successfully made an application but whenever I try installing it in the
I have made the following linked list which is not printing the list .The
I have an iPhone app that is using CoreData. I recently made some minor
I have a categories model made with the fantastic awesome_nested set. I have successfully

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.