Home > Back-end >  How to add string inside array using bash shell?
How to add string inside array using bash shell?

Time:06-03

I'm using bash shell and have an array like this:

Numbers=$( one two three four five )

And I need to make it like this using sed probably but I'm not really sure:

Numbers=$(Numbers:one,Numbers:two,Numbers:three,Numbers:four,Numbers:five)

I tried it with some commands but the "Numbers:" only applied to first word and not all.

CodePudding user response:

i Dont know if i understood what u said but

u have to push object inside your array for this case:

[{ 'Numbers' : 'one'},{ 'Numbers' : 'two'},{ 'Numbers' : 'three'}]

CodePudding user response:

Question is unclear and confusing.

Converting one two three four list

echo "one two three four"|awk 'BEGIN{ORS=RS=" "}{print "number:"$0}'


number:one number:two number:three number:four

generating an array from converted list

Numbers=($(echo "one two three four"|awk 'BEGIN{ORS=RS=" "}{print "number:"$0}'))

printf "%s\n" "${Numbers[@]}"
number:one
number:two
number:three
number:four
  • Related