Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

How to add a new line to a bash array?

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
I am a nobody
Posts: 45
Joined: 2012-04-13 03:44

How to add a new line to a bash array?

#1 Post by I am a nobody »

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

User avatar
Head_on_a_Stick
Posts: 14114
Joined: 2014-06-01 17:46
Location: London, England
Has thanked: 81 times
Been thanked: 132 times

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

#2 Post by Head_on_a_Stick »

deadbang

User avatar
debiman
Posts: 3063
Joined: 2013-03-12 07:18

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

#3 Post by debiman »

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

Post Reply