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:https://www.jiyik.com/en/xwzj/opersys_10030.html

Related Articles

Creating a Progress Bar in Bash

Publish Date:2025/03/23 Views:94 Category:OPERATING SYSTEM

A progress bar is a visual indicator that shows the progress of a task, such as a long-running script or command. It can be used to provide feedback to the user about the status of a task and can also help estimate the time remaining before

String comparison in batch files

Publish Date:2025/03/22 Views:168 Category:OPERATING SYSTEM

A string is an ordered collection of characters. You can compare strings using conditional commands in batch files, namely, if, if-else, and for commands. Strings may contain spaces and special characters, which can cause errors in the batc

Remove double quotes from variable in batch file

Publish Date:2025/03/22 Views:177 Category:OPERATING SYSTEM

In batch files, variables containing multiple words or spaces must be placed in double quotes, but sometimes, we do not want to see these quotes in the output. These quotes can be removed from the variables in batch files. There are many wa

Reading file into variable in batch script

Publish Date:2025/03/22 Views:121 Category:OPERATING SYSTEM

Sometimes, we need to put the entire contents of a file into a variable for various purposes, such as finding specific data from a file, replacing a specific part of a file, etc. In Batch, it is very easy to put the entire file contents in

Print a file after skipping first X lines in Bash

Publish Date:2025/03/22 Views:187 Category:OPERATING SYSTEM

Suppose you have a file, a large file, and you want to display its contents. How would you do it? You obviously don't want to print out the entire contents of the file, as that's not very practical. You might want to print some selective li

Get the Primary IP Address in Linux

Publish Date:2025/03/22 Views:70 Category:OPERATING SYSTEM

There are various ways to get network details in Linux. We will learn some of them in this article. This simple guide is all about using different commands that can be used to get the primary IP address in Linux operating system using Bash

Bash History Size

Publish Date:2025/03/22 Views:189 Category:OPERATING SYSTEM

In this article, we will learn about Bash history, its size, and how we can change our history size and handle limits. Before getting into our topic, let us first understand why we need history in Bash shell and how we can get it. Most deve

Find Current Folder Name in Bash

Publish Date:2025/03/22 Views:107 Category:OPERATING SYSTEM

Finding a directory is very easy through Bash scripting. But finding the exact directory folder name you are in right now is a bit complicated. This article will introduce three methods to find the folder name from this article directory. I

Scan to Read All Tech Tutorials

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

Recommended

Tags

Scan the Code
Easier Access Tutorial