declare

Shell Scripting Tutorial for Beginners 14 - Array variables



Sharing buttons:

hey guys welcome to the next video on

shell scripting tutorial for beginners

in this video we will see how we can use

arrays in shell scripting now bash

suppose simple one-dimensional arrays

and to declare arrays you just need to

use the name of the array so whatever

name you want to give to your array

variable just give it for example you

want to have operated system array

variables so I will just write OS is

equal to this is the name of the array o

X right and then you just use this

parent ever and in the single quotes you

just add the element to the array so for

example I will just add Ubuntu here and

then separate it by a space no comma you

just need to add things a second the

elements for example windows and so on

so third for example we will add Kali

Linux or Kali right and just save it and

to print this array you just need to use

echo and I think in the first few videos

we have seen how we can print array but

we will repeat this thing so just use

dollar symbol and then in the curly

bracket name of the array or a name is

OS and then in the square bracket use

the special character at the rate which

is going to print the elements of the

array so let's run the script and let's

see what happens so you can see it

prints the elements of the array right

now in order to print any specific

element you just need to use echo and

once again dollar symbol curly brackets

name of the array and the square bracket

the index of the element which you want

to print so for example I want to print

Windows which is the first element and

how it's the first element or for the

first index the

element or index of the array starts

from the 0th position so this is the

index 0 this is the index 1 and this is

at the index 2 right

so index 1 is assigned to the windows

element right so let's run the script

once again and let's see what happens so

it prints windows if you right here 0th

index it's going to print ubuntu right

and let's go ahead and i'm going to show

you how you can print the indexes or

indices of the array so you just need to

use once again the dollar symbol and the

curly bracket and the name of the array

and once again in the square bracket use

the add the rate but this time here you

just need to use not symbol this

exclamation mark okay and let's see what

happens when we run the script and you

can see it trains the indexes of the

array or indices of the array right so

we have 3 element which starts from 0 1

2 so it prints 0 1 2 ok now for example

you want to print the length of the

array what we will do we will just write

echo and once again echo and then the

dollar symbol once again and then the

square bracket name of the array and in

the square bracket you just need to

write at the rate special character and

in front of the array variable name you

just need to add this hash symbol okay

and this means you want to print the

length of the array okay so let's run

the script and let's see what happens so

you can see the array length in our case

is 3 because it contains 3 elements

right now how we can add elements to the

array so to add elements

you just need to write the name of the

array and then whatever index at

whatever index you want to add an

element you just need to write the index

of that element so for example I want to

add at the index 3 some other OS name so

for example I want to write Mac here

okay so this will be added to the third

index of this array now let's see what

happens when we run the script you can

see here this fourth element is added to

an array at the index three right so in

this way you can add the element now if

you want to update this so for example I

right here at the 0th index then this

Mac will be assigned at the 0th index

which is going to replace this ubuntu by

Mac right so let's run the script and

you can see now it has at the 0th index

Mac then Windows then Kali okay so if

you want to update it you just need to

write the index of the element which you

want to replace the new element with and

then you just assign the element value

okay now we will see how we are going to

remove how can we remove element from an

array so to remove an element from an

array what we will do we will use the

keyword unset so just write unset and

then the name of the array so money

array name is where and then whatever

index of the element you want to remove

so for example I want to remove the

index at which Kali Linux is let me just

edit this index also so Mac is at the

third index right and Kali is at the

second index so 0 1 2 ok so I am going

to remove

the callee from my array right so just

write the index value of the element

which you want to remove and then once

again run the script and you can see now

we have open to Windows and Mac and no

Colleen right now you may ask why it's

printing like this because we already

have removed the element at index two

right so it's like that that in bash

you can add element at any index so it

can be for example at fixed index and

nothing is going to happen it's going to

print the elements of the array in the

sequence of the index right so if it

it's at the fixed index it will be

sorted in the ascending order so 0 1 and

then if it it it is at the 6th position

or index and it's going to just print

the value at the fixed index and now

this position will be ignored so this

means that some members of the array can

be left uninitialized and gaps in the

arrays are okay now bash permits arrays

operation on variables also even if the

variables are not explicitly declared as

an arrays so let's take an example so I

have a variable name called string for

example and I assign any value to it so

let's say this random string right

now when I I will just take this echo

come on here and then I will just print

the string right string array and let's

see what happens so let me clear the

terminal and let's run the script once

again and you can see here it prints the

you know array or string value as it is

right so you can see string is printed

right so

taking this string as an array so you

can use this kind of notation also but

let's print for example the 0th index of

the array so once again I'm going to

just write 0 here instead of at the rate

I will just write 0 and then once again

run the script and it prints the same

string value right let me just print the

index 1 value and here the magic happens

you can see at index 1 there is nothing

so why this is happening because you are

going to treat any variable as an array

the array element value at 0 10 Dex

whatever the value of the variable it's

assigned at the 0th index right so you

can treat any variable as an array but

the value of this array will be assigned

to the 0th index okay so at the first

index there is nothing because the whole

value is assigned to the 0th index and

this we can prove by printing the length

of the array also so let's print the

length of this string array and let's

see what happens so you can see the

length of the string array is only 1

which is this one ok so this is how you

can use arrays in bash scripting I hope

you've enjoyed this video please rate

comment subscribe and bye for now