Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7777385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:07:43+00:00 2026-06-01T18:07:43+00:00

I have a bash script that needs to build some sets each containing 1

  • 0

I have a bash script that needs to build some sets each containing 1 element from multiple queues (i’m considering using arrays to store these)
For example if i have the following queues:

array1=(a b c e f)                                                            
array2=(x y z)                                                                
array3=(1 2 3 4)

I need to obtain the following arrays (not necessarily all at once, i process the resulting arrays one at a time)

a x 1
b y 2
c z 3
e 4
f

The number of arrays is dynamic.

So basically i need to pop the first element from each array and put in another array.
I know i can pop the first element from an array like this:

 el1=${array1[0]}
 array1=(${array1[@]:1:$((${#array1[@]}))})

Also, i know i can extract a value from an indirectly referenced array like this:

val=$(eval echo \${$arr[0]})

My question is: How can i rewrite the part that pops the first element from an array to use indirect references.

Here is the full test script:

#!/bin/bash

set -e 
set -u


array1=(a b c e f)
array2=(x y z)
array3=(1 2 3 4)

finished=false
while ! $finished; do
   finished=true

   array4=()
   for arr in array1 array2 array3; do
      val=$(eval echo \${$arr[0]})
      if [ ! -z $val ]; then
         array4=(${array4[@]} $val)
         $arr=(\${$arr[@]:1:$((\${#$arr[@]}))})
      fi
   done

   l4=${#array4[@]}
   if [ $l4 -gt 0 ]; then
      finished=false

      for i in ${array4[@]}; do
         echo $i
      done
   fi

done
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T18:07:44+00:00Added an answer on June 1, 2026 at 6:07 pm
    array1=(a b c e f )                                                            
    array2=(x y z)                                                                
    array3=(1 2 3 4)
    
    cnt=0
    while : ; do
      line=''
      for a in ${!array*}; do               # loop over names "array*"
        val=$(eval echo \${$a[$cnt]} )
        line="$line ${val:- }"              # build line
      done
      ((cnt++))
      [[ $line =~ ^\ +$ ]] && break         # break if line has only blanks
      echo -e "${line:1}"
    done
    

    Output:

    a x 1
    b y 2
    c z 3
    e   4
    f      
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a script in Bash called Script.sh that needs to know its own
I have a bash script that sources contents from another file. The contents of
I have a bash script that installs some software. I want to fail as
I have a bash script that I need to write my password to run
I have a bash script that creates a Subversion patch file for the current
I have a bash script that runs a simulation program written in Fortran 90,
I have a bash script that does ssh to a remote machine and executes
I have a bash script that cuts out a section of a logfile between
I have a bash script that simply calls different calls and redirect stdout and
I have a bash script that uses growlnotify to send notifications. However, growlnotify doesn't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.