-1

I have a bash script like:

#!/bin/bash

tom[0]="28 years old, computer science bs"
tom[1]="41 years old, physics bs"

ryan[0]="32 years old, technician"

mary[0]="25 years old, math bs"
mary[1]="30 years old, chemistry ba"

robert[0]="34 years old, math bs"

for name in robert mary tom ryan  
do  
    echo "name = ${name}:" 
    for who in "${name[@]}"
    do  
        echo "age,degree = ${!name}"  
    done
done

exit 0

output:

name = robert:
age,degree = 34 years old, math bs
name = mary:
age,degree = 25 years old, math bs
name = tom:
age,degree = 28 years old, computer science bs
name = ryan:
age,degree = 32 years old, technician

How do I modify my bash script to display below output:

name = robert:
age,degree = 34 years old, math bs

name = mary:
age,degree = 25 years old, math bs
age,degree = 30 years old, chemistry ba

name = tom:
age,degree = 28 years old, computer science bs
age,degree = 41 years old, physics bs

name = ryan:
age,degree = 32 years old, technician

17
  • 4
    It's not clear what you mean. Just do echo mary or echo tom. Commented May 30, 2023 at 20:00
  • 1
    It seems like you probably want an associative array, not multiple array variables. info['tom0']="28 years old, computer science bs"; info['mary0']="25 years old, math bs"; info[mary1]="30 years old, chemistry ba"; Commented May 30, 2023 at 20:04
  • 2
    Unfortunately, bash doesn't have 2-dimensional arrays. It's probably not the best language to use for an application like this. Commented May 30, 2023 at 20:06
  • 3
    It sounds very much like you have the wrong approach to whatever you're trying to do. If you ask a question about THAT, including a minimal reproducible example with concise, testable sample input, expected output, and code, then we can help you better than showing you how to keep going down your current path. Commented May 30, 2023 at 20:56
  • 2
    @novi2023 we all understood what you wanted to do, it wasn't confusing, but we wanted to help you come up with a better approach as you seemed to be asking an XY Question. Commented May 30, 2023 at 22:23

1 Answer 1

0

Bash arrays are tricky. While "${!name}" gives you access to a scalar variable by name (or to an array’s zeroth element), you cannot access array elements other than [0] directly this way. For example, the construct "${!name[@]}" would expand to the array’s indices rather than to the stored values.

BTW, "${name[@]}" makes little sense in your code; it is equivalent to "$name" in this special case, because a scalar string "$name" is also (implicitly) "${name[0]}" which (in absence of other array elements) equals "${name[@]}".

To access Bash arrays by name, you need almost-pointers in the form of declare -n. That gives you an alias which behaves (almost) like the referenced variable (array in this case):

tom=('28 years old, computer science bs'
     '41 years old, physics bs')
ryan=('32 years old, technician')
mary=('25 years old, math bs'
      '30 years old, chemistry ba')
robert=('34 years old, math bs')

for name in robert mary tom ryan; do
  printf '\nname = %s:\n' "$name"
  declare -n array="$name"
  printf '%s\n' "${array[@]}"
done

In the snippet above one could also loop over "${array[@]}" using a for-loop, but one can just as well let printf handle a few extra arguments.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you @Andrej for spending time to explain the differences of how to access the bash arrays. I appreciate it. I can use your solutions, I just have to re-arrange the data ( index arrays) passed down to me. I still would prefer not to alter the original data, but if I can't find any other way, then I will re-arrange those given data.
Your solutions actually worked. After I played around with it, I didn't have to modify any of the arrays given at all. I used for loop and it actually displayed what I was trying to accomplish. Thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.