How to Create Bash Multiline Array [Solved]

How to Create Bash Multiline Array [Solved] - linuxnasa

In this tutorial, we will learn about how to create bash multiline array. In any programming language, array is a common and most usable data structure as it allows us to store multiple elements. There are different ways to create and process arrays in different programming languages. We will talk about array in bash scripting today, specifically about multi-line array in bash scripting. So let us look at some important aspect of array.

 

Multiline Array Overview

In Bash, array is a data structure which is used to store multiple elements in a array type variable. When you work with bash scripting, it becomes necessary to have the deep understanding of various data structures, including array. Array allows us to store multiple elements under a single variable which makes the data management and manipulation easy in bash scripting. There are two types of array which we can use in bash scripting. These are:

  • Single line array
  • Multi line array

We use multi line array when we deal with complex data types. We will learn about how multi-line arrays are created and processed in bash scripting in the upcoming sections of this tutorial. So let’s get started.

 

How to Create Bash Multiline Array [Solved]

Also read: What is 10 Months from Today [Code in 5 Programming Languages]

To get the deep understanding of how multi-line arrays are created and processed in bash scripting, let us write a code and understand each aspect of multi-line array. In the below example code, we have defined a multi-line array which store all the planets. We are printing the planets from and and lastly deleting the elements from array. Let us break all these operations and understand how exactly this is working.

#!/bin/bash

planets=(
"Mercury "
"Venus"
"Earth"
"Mars"
"Jupiter"
"Saturn"
"Uranus"
"Neptune"
)

echo "Printing planets from multi-line array:" 
echo "${planets[@]}"

echo "Iterating through multi-line array and printing planets: "
for planet in "${planets[@]}":
do
echo "$planet"
done

echo "Deleting elements from multi-line array"
unset planets
echo "Elements in Array: ${planets[@]}"

OUTPUT

Printing planets from multi-line array:
Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune

Iterating through multi-line array and printing planets: 
Mercury 
Venus
Earth
Mars
Jupiter
Saturn
Uranus
Neptune

Deleting elements from multi-line array
Elements in Array:

 

1. Create Multi-line Array 

To create multi-line array in bash scripting, we follow the below syntax. We will create a multi-line array called “planets” and store all the planet’s name in it. This is done in following way as shown in the example below.

Syntax:

array_name=(
    element1
    element2
    element3
    ........
    elementN
)

To create multi-line array

planets=(
"Mercury"
"Venus"
"Earth"
"Mars"
"Jupiter"
"Saturn"
"Uranus"
"Neptune"
)

 

2. Access Multi-line Array

So we have created a multi-line array of planets. Let us now print all the planets on the standard output (i.e on console) using below commands in bash scripting.

echo "Planets in Milky Way Galaxy are: " 
echo "${planets[@]}"

OUTPUT

Mercury Venus Earth Mars Jupiter Saturn Uranus Neptune

 

3. Iterate through Multi-line Array

In the previous example, we simply printed all the planet names from the array. But what if we require to iterate the array and read each element one by one? . We can do this operation by iterating through the multi-line array using for loop as shown below.

for planet in ${planets[@]}:
do 
echo "$planet"
done

OUTPUT

Mercury
Venus
Earth
Mars
Jupiter
Saturn
Uranus
Neptune

 

4. Deleting Elements in Multi-line Array

If we want to reset the defined multi-line array, we can do that by using the command ‘unset’ in bash scripting. In the below code, we are unsetting the array ‘planets’ which ultimately deletes all the elements stored in it. So when you print the array, it will return an empty array as shown below.

unset planets
echo "Elements in Array: ${planets[@]}"

OUTPUT

Elements in Array:

 

Summary

Reference bash array man page.

Leave a Comment