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.