Shell Scripts Part 5 - Array

Pubblicato il: 13 ottobre 2020
sul canale di: Virtu Mentor
184
3

Hello Everyone,

Welcome to VIrtuMentor!!

Today, we will learn about an Array’s in shell scripts.
An Array is variable in shell script which hold multiple values with same or different data types.
Normally, in shell script everything considered as string.

First we create a script called array.sh and add shy bang value
#!/bin/bash

There are three ways to declare array.
1. Indirect
2. Explicit
3. Compound

Let’s declare an array.

In Indirect, we declare an array as:
Virtu[0]=“Mentor”

In this Virtu is an array name
0 is Index number and “Mentor” is value of an array

In array the Index start with zero and goes to n-1
means if we have 10 element in array then the index of last element is 9
as index start with zero

In Explicit, we first declare array and then assign values to it
To declare it we use declare keyword

declare -a Virtu

In Compound, we can declare array with values like

Virtu=(Mentor 21 Linux Bash Scripts)

We can also add new values to array later.

In this tutorial, we are using compund array
So lets comment Indirect and Explicit array declared above

Now we will see how to print the value of array

To print all elements of array we use
echo ${Virtu[@]} and
echo ${Virtu[*]}

We can also print selective index value
To print first element
echo ${Virtu[0]}
echo ${Virtu}

To print selected index value
echo ${Virtu[3]}
echo ${Virtu[1]}

We can also print the length of element in array
To print length we use ‘#’

echo ${#Virtu[0]}

This is all about an Array in bash script.
If you like my video, please do like, share and subscribe to VirtuMentor
and comments for any suggestions

Thank you!


In questa pagina del sito puoi guardare il video online Shell Scripts Part 5 - Array della durata di ore minuti seconda in buona qualità , che l'utente ha caricato Virtu Mentor 13 ottobre 2020, condividi il link con amici e conoscenti, su youtube questo video è già stato visto 184 volte e gli è piaciuto 3 spettatori. Buona visione!