0

The code:

arr=()
arr+=( USA,1 )
arr+=( Chile,20 )
for row in ${arr[@]};
do
    echo place: ${row%%,*} finished: ${row##*,}
done

With Bash 4.2.46, how would I pass this array "arr" to a function, then receive it in the function and parse it with the for loop?

Nic3500
  • 6,706
  • 10
  • 30
  • 37
KDog
  • 1
  • 1
  • 1
    Please add to your question (no comment): What have you searched for, and what did you find? What have you tried, and how did it fail? – Cyrus Jun 03 '22 at 22:36
  • 1
    Bash doesn't have multi dimensional arrays. – Shawn Jun 03 '22 at 22:37
  • 1
    adding to Shawn's comment: what you've defined in a one-dimensional array where the values are comma-delimited strings, ie, `typeset -p arr` => `declare -a arr=([0]="USA,1" [1]="Chile,20")` – markp-fuso Jun 03 '22 at 22:42
  • 1
    as for the question about passing an array to a function ... quite easy with `bash 4.3+` where you have access to nameref's (eg, [see this answer](https://stackoverflow.com/a/68627968)) ... any chance you can upgrade to a newer version of `bash`? – markp-fuso Jun 03 '22 at 22:47
  • I removed all the empty lines from your code and tested it in bash, and I get this output: **line 1** `place: USA finished: 1` **line 2** `place: Chile finished: 20`. So what is the issue exactly?!? – Nic3500 Jun 04 '22 at 01:57

0 Answers0