Fake a multi-dimensional array

Lucas

As bash does not support multi-dimensional arrays, how can I fake it so I could access it like this:

#declare
array["foo"] = "bar"

#print
echo array["foo"] //how to display declared 'bar' here?

So the question is: what I need to do, to print out the bar when accessing array["foo"]?

konsolebox

You simply need to use associative arrays:

declare -A array=()

#declare
array["foo"]="bar"

#print
echo "${array["foo"]}"

And you can fake multi-dimensional arrays with it like

i=1
j=2
array[$i,$j]=1234
echo "${array[$i,$j]}"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related