JIYIK CN >

Current Location:Home > Learning > OPERATING SYSTEM >

Multidimensional arrays in Bash

Author:JIYIK Last Updated:2025/03/23 Views:

Multidimensional array is a very important element for any program. It is mainly used to create table view of data and many other purposes.

This article demonstrates how to create a two-dimensional array. In addition, we will discuss the topic with necessary examples and explanations.

We will discuss two different approaches here.


Basic method of declaring multidimensional arrays in Bash

This is the most basic way to create a multidimensional array. In the following example, we will create a very basic two-dimensional array.

Below is the code for our example.

ArrayOfArray_1=("Alen" "24")
ArrayOfArray_2=("Walker" "31")
MainArray=(
  ArrayOfArray_1[@]
  ArrayOfArray_2[@]
)

ArrayLength=${#MainArray[@]}
for ((i=0; i<$ArrayLength; i++))
do
  name=${!MainArray[i]:0:1}
  age=${!MainArray[i]:1:1}
  echo "Name : ${name}"
  echo "Age : ${age}"
done

The code above shows that we declared two different arrays named ArrayOfArray_1 and ArrayOfArray_2. After that, we declared our main array whose elements are the two arrays we recently declared.

We then calculated the array length in a loop. Inside the loop, we extracted the data from both sub-arrays by indexing into the main array.

Finally, we echo the final result. After executing the script, you will get the output as shown below.

Name : Alen
Age : 24
Name : Walker
Age : 31

Declaring a 2D Array Using Associative Arrays in Bash

This method is a little more complicated. This method discusses a special type of Bash script array called an associative array.

An associative array is a special kind of array that can store string values ​​as keys or indexes. It is similar to other programming languages.

The general syntax for declaring an associative array is declare -A ArrayName.

The following example will create a two-dimensional array with five rows and six columns. Below is the code for our example.

declare -A Array2D
RowNum=5
ColumnNum=6

for ((i=1;i<=RowNum;i++)) do
    for ((j=1;j<=ColumnNum;j++)) do
        Array2D[$i,$j]=$RANDOM
    done
done

f1="%$((${#RowNum}+1))s"
f2=" %9s"

printf "$f1" ''
for ((i=1;i<=RowNum;i++)) do
    printf "$f2" $i
done
echo

for ((j=1;j<=ColumnNum;j++)) do
    printf "$f1" $j
    for ((i=1;i<=RowNum;i++)) do
        printf "$f2" ${Array2D[$i,$j]}
    done
    echo
done

In the above example, we declared an associative array called Array2D.

After that, we created two variables and assigned them an integer value. This integer value specifies the number of rows and columns.

We then created a nested loop to fill the array with random numbers.

Now that we have completed the declaration and organization of our array, it is time to see what our two-dimensional array will look like.

We do this by using some loops as shown in the code. When you execute the above script, you will see the output as shown below.

           1         2         3         4         5
 1     16700      5241      2599     24330      1662
 2     23264     19557     10425     13413     25606
 3     17987      4199     13598     23897     26734
 4     24420     27830     24855      8165     13531
 5     15495     18790     13347     12947     11826
 6     23458     22838       137     32454     32441

All the code in this article is written in Bash. It will only work in the Linux Shell environment.

For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.

Article URL:

Related Articles

How to decompress x.tar.xz format files under Linux

Publish Date:2025/04/08 Views:186 Category:OPERATING SYSTEM

A lot of software found today is in the tar.xz format, which is a lossless data compression file format that uses the LZMA compression algorithm. Like gzip and bzip2, it supports multiple file compression, but the convention is not to compr

Summary of vim common commands

Publish Date:2025/04/08 Views:115 Category:OPERATING SYSTEM

In Linux, the best editor should be vim. However, the complex commands behind vim's powerful functions also make us daunted. Of course, these commands do not need to be memorized by rote. As long as you practice using vim more, you can reme

Detailed explanation of command return value $? in Linux

Publish Date:2025/04/08 Views:58 Category:OPERATING SYSTEM

? is a special variable. This variable represents the return value of the previous command. That is to say, when we run certain commands, these commands will return a code after running. Generally, if the command is successfully run, the re

Common judgment formulas for Linux script shell

Publish Date:2025/04/08 Views:159 Category:OPERATING SYSTEM

In shell script programming, predicates are often used. There are two ways to use predicates, one is to use test, and the other is to use []. Let's take a look at how to use these two methods through two simple examples. Example 1 # test –

How to use the Linux file remote copy command scp

Publish Date:2025/04/08 Views:151 Category:OPERATING SYSTEM

Scp copies files between two hosts over the network, and the data is encrypted during transmission. Its underlying layer uses ssh for data transmission. And it has the same authentication mechanism and the same security level as ssh. When u

Scan to Read All Tech Tutorials

Social Media
  • https://www.github.com/onmpw
  • qq:1244347461

Recommended

Tags

Scan the Code
Easier Access Tutorial