How to add a new line to a bash array?

Need help with C, C++, perl, python, etc?

How to add a new line to a bash array?

Postby I am a nobody » 2018-08-09 10:50

How can I add a new line to following bash array? The get_group is a function, it assigns a list of members in a group to the GROUP, but the statement GroupList+=${GROUP} stacks the end of the first group and the start of the second group in one line, I tried to add new line to the array, that does not work at all

declare -a GroupList=()
for i in $(seq 1 3); do
GROUP=$(get_group ${i})
GroupList+=${GROUP}
GroupList+="\n" # does not work
done
I am a nobody
 
Posts: 42
Joined: 2012-04-13 03:44

Re: How to add a new line to a bash array?

Postby Head_on_a_Stick » 2018-08-09 11:15

dbruce wrote:Ubuntu forums try to be like a coffee shop in Seattle. Debian forums strive for the charm and ambience of a skinhead bar in Bacau. We intend to keep it that way.
User avatar
Head_on_a_Stick
 
Posts: 8315
Joined: 2014-06-01 17:46
Location: /dev/chair

Re: How to add a new line to a bash array?

Postby debiman » 2018-08-11 11:17

afaiu, you don't really need the actual newline, but need to make sure that each
Code: Select all
GroupList+=${GROUP}
adds one more member to the array, yes?

i have never tried the '+=' syntax on arrays;
you can see if it worked like this:
Code: Select all
for (( i=0;i<${#GroupList[@]};i++ )); do
    echo "GroupList[$i]: ${GroupList[i]}"
done
User avatar
debiman
 
Posts: 3064
Joined: 2013-03-12 07:18


Return to Programming

Who is online

Users browsing this forum: No registered users and 4 guests

fashionable