4

My bash script:

#!bin/bash
MY_ARRAY=("Some string" "Another string")
function join { local IFS="$1"; shift; echo -e "$*"; }
join "," ${MY_ARRAY[@]}

I want the output to be: Some string,Another string.

Instead, I get Some,string,Another,string.

What must I change to get the result I want?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
Username
  • 801
  • 3
  • 21
  • 38

1 Answers1

10

My modified version of your script:

#!bin/bash
my_array=("Some string" "Another string")
my_join() {
  [ "$#" -ge 1 ] || return 1
  local IFS="$1"
  shift
  printf '%s\n' "$*"
}
my_join , "${my_array[@]}"

Notes:

Wildcard
  • 35,316
  • 26
  • 130
  • 258