Bash

如何加入每個字元串都有空格的字元串數組?

  • August 31, 2017

我的 bash 腳本:

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

我希望輸出為: Some string,Another string.

相反,我得到Some,string,Another,string.

我必須改變什麼才能得到我想要的結果?

我修改後的腳本版本:

#!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[@]}"

筆記:

引用自:https://unix.stackexchange.com/questions/389369