1

EDIT 1: about Multi-dimensional arrays in Bash, as far as I can understand, all the answers and examples are about associative arrays, not numeric multidimensional arrays like in case bellow. They have named indexes with just ONE value, not numeric indexes with more than ONE value.

EDIT 2: According to this answer, perhaps it may be impossible to do what I want in Bash for I could not do something like: echo $A[1][1] to get the second value of the second index in the array, like we usually could in PHP and others.

EDIT 3: According to this other question, I am almost sure this cannot be done in Bash in a "human readable" way.


In PHP this is how a numeric multidimensional array is created:

<?php
$b[]=array("Apple's Mac","UNIX from Ken, Dennis, Douglas and Peter");
$b[]=array("Sun Microsystems' Solaris","Linus Torvalds' Linux");

//or:

$b=array(
    array("Apple's Mac","UNIX from Ken, Dennis, Douglas and Peter"),
    array("Sun Microsystems' Solaris","Linus Torvalds' Linux")
);

print_r($b);

/*Array
(
    [0] => Array
        (
            [0] => Apple's Mac
            [1] => UNIX from Ken, Dennis, Douglas and Peter
        )    
    [1] => Array
        (
            [0] => Sun Microsystems' Solaris
            [1] => Linus Torvalds' Linux
        )    
)*/

//To access the first numeric array:

print_r($b[0]);

?>

How can I make this in Bash? This is what I tried so far:

A=(
"Apple's Mac" "UNIX from Ken, Dennis, Douglas and Peter"
"Sun Microsystems' Solaris" "Linus Torvalds' Linux"
)

#Trying to print the second array inside array ${A[@]}...
echo ${A[1]}
#It prints:
UNIX from Ken, Dennis, Douglas and Peter

#The same if I do:

X=("Apple's Mac" "UNIX from Ken, Dennis, Douglas and Peter")
Y=("Sun Microsystems' Solaris" "Linus Torvalds' Linux")
A=(
"${X[@]}"
"${Y[@]}"
)
echo ${A[1]}
#It prints:
UNIX from Ken, Dennis, Douglas and Peter

The only "hackish" way I could find to get I want:

A=(
"Apple's Mac|UNIX from Ken, Dennis, Douglas and Peter"
"Sun Microsystems' Solaris|Linus Torvalds' Linux"
)

#Printing the second index of the array
echo ${A[1]}
#It prints:
Sun Microsystems' Solaris|Linus Torvalds' Linux

#Accessing the second "value" of the second index
echo ${A[1]} | cut -d'|' -f2
#It prints:
Linus Torvalds' Linux

But there MUST be an easier, more correct, proper way of doing the same...

Thanks for any help.

Community
  • 1
  • 1
Roger
  • 8,286
  • 17
  • 59
  • 77
  • 1
    Yup it will, `A` becomes a multi-element array with `X` and `Y` contents, whose second element is the one you printed – Inian Jan 19 '17 at 13:59
  • Yes but this is different from what I want to get. The second index of the bash array must be an array, the same way I get from the PHP code. – Roger Jan 19 '17 at 14:15
  • Possible duplicate of [multi-dimensional arrays in BASH](http://stackoverflow.com/questions/11233825/multi-dimensional-arrays-in-bash) – Jose Ricardo Bustos M. Jan 19 '17 at 14:26
  • As I said above: "as far as I can understand, all the answers and examples are about associative arrays, not numeric multidimensional arrays like in case bellow. They have named indexes with just ONE value, not numeric indexes with more than ONE value." – Roger Jan 19 '17 at 15:17

1 Answers1

0

Not the best but here's another solution :

you can use declare like the following

#!/bin/bash

A1=("Apple's Mac" "UNIX from Ken, Dennis, Douglas and Peter")
A2=("Sun Microsystems' Solaris" "Linus Torvalds' Linux")

declare -p A1 A2 > multi-array.save

and if you need to load the different arrays back :

source ./multi-array.save

Of course, that's not multi-dimensional but for memory's sake it gets something similar.

vdegenne
  • 12,272
  • 14
  • 80
  • 106