-1

I have an array as look like below

data=(1,2,3,4,5)

${data[*]}

Output:

1 2 3 4 5

The above output like \n

I just want to use for loop and append each value in variable with comma separated as look like output as below:

testData=1,2,3,4,5

Any idea how can I achieve this only using FOR loop in shell script.

ArrchanaMohan
  • 2,314
  • 4
  • 36
  • 84
  • 1
    Show us the output of `declare -p data`. (Because comments have lousy formatting, [edit] your question and put the output there.) – John1024 Feb 10 '19 at 17:50
  • As per your update to the question, your array `data` has only __one__ element. Is that what you intended? – John1024 Feb 10 '19 at 18:00
  • Yes. I just want to print that one element as comma separated using for loop in shell – ArrchanaMohan Feb 10 '19 at 18:02
  • I'm working on some other work around for that. If I get input it would be very helpful. right now I'm just getting the value and manipulating that using java. but it would be great if I get solution for above query which i can directly use it. – ArrchanaMohan Feb 10 '19 at 18:03
  • 1
    If the array is only going to have one element, why are you using bash arrays? They offer no value in this case. What are your rules for "splitting" the one element? Do you want a comma after every character? – John1024 Feb 10 '19 at 18:05
  • Sorry for misconception. Its my fault. Updated the query. – ArrchanaMohan Feb 10 '19 at 18:11
  • 1
    `data=(1,2,3,4,5)` is identical to `data=( "1,2,3,4,5" )` -- which is to say, it remains a single-element array. `echo ${data[*]}` certainly does not echo `1 2 3 4 5` in that case, unless you have `IFS=,` (in which case the results are split into separate arguments to `echo` *after* expansion is done, *before* `echo` is started). – Charles Duffy Feb 10 '19 at 18:12
  • 1
    ...to have a 5-item array, it would need to be `data=( 1 2 3 4 5 )`. – Charles Duffy Feb 10 '19 at 18:15
  • ...though if `IFS=,` is true, and your original definition really is `data=(1,2,3,4,5)`, all you'd need to do to print the value with commas is to add quotes, changing `echo ${data[*]}` to `echo "${data[*]}"`. – Charles Duffy Feb 10 '19 at 18:25
  • @ArrchanaMohan I think we would be much better able to help you if you provided some context. Where is the data coming from? Is it user input? Another program? And, for what reason do you want commas in it? Is it for output formatting? Is it to pass the info to another program? – John1024 Feb 10 '19 at 18:44

2 Answers2

1

If your input truly were an array, as in:

data=( 1 2 3 4 5 )

...converting it to a comma-separated list of values might look like:

testData=$(IFS=,; printf '%s\n' "${data[*]}")

...or, with more of an eye to efficiency:

printf -v testData '%s,' "${data[@]}"
testData=${testData%,}

after either of which,

echo "$testData"

will emit:

1,2,3,4,5
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
0

Using bash parameter transformation and substitution:

x=(1 2 3 4 5)  x=${x[@]@P}  x=${x// /,} ;  echo $x

Output:

1,2,3,4,5
agc
  • 7,973
  • 2
  • 29
  • 50
  • But this doesn't use `for` like the OP wanted. And the rest of the `${x[@]}` array is still there. – agc Feb 10 '19 at 19:32
  • ...that said, without a clearly-elaborated reason to do so, the "using a for loop" restriction is hardly *practical*, as required by [our site's scope restrictions](https://stackoverflow.com/help/on-topic). Asking for a `for` loop to construct something that parameter expansion supports is asking for worse performance, and typically a reduction in terseness to boot. – Charles Duffy Feb 10 '19 at 23:55
  • 1
    @CharlesDuffy, Thanks; what you say is true... though my comment about not using `for` was meant somewhat waggishly. This weekend I've answered a few Q.s that sound a lot like homework, and my short answers would probably be more work for a certain kind of student, and also could annoy any over-literal test-giver. Hmm. `s/waggish/passive aggressive/`? – agc Feb 11 '19 at 01:53