01 Shell Script Arrays Tutorial

Sharing buttons:


hello and welcome this is you know we've

just started a series on shell scripts

for 2017 we do a lot of these and we're

going to do a lot of short tutorials and

build upon them and today we're just

going to look at a raise in a way we're

going to look at just looping through an

entire array so let's go ahead and use

them as my texture if it has always use

whatever texture you're comfortable with

and I'm going to start off by using

giving it the shebang line

bin bash now let's go ahead and create

an array so an array in bash is pretty

simple we got to give it a name I'll

just call it array here of course in a

real script you want to give it more of

a name that tells you what it is and

here we can give it things like dog cat

fat Mouse and that is our array and we

can call it you know if we wanted to

echo it out we could do something like


echo dollar sign and then inside these

squiggly braces or curly braces whatever

you call it

alcohol array and then we'll give it the

number of the array we want so we have

these arrays here I'm going to put in

one I'm going to save that now it's a

shell script we have to make it

executable give it permission to run we

only have to do this once on our system

once we do that we do dot slash dot

slash again just means we're running a

script in the current directory not a

system program from our path directory

so let's go ahead and run that and as

you can see we got cat now you might go

why did it do dog because arrays start

at zero as they should so we want dog

and can say zero and of course we go in

there and I'll copy this line and I can

do two one or two three one oops let's

go ahead and fix there we go so it

should go dog bat mouse cat if I'm

looking up that directly on it there we


now let's say we want to loop through

all of them we want to do something with

each of them we can do this go ahead and

leap these

for the most part we're going to create

a loop of four loops I'll say four and

then a variable and give it any variable

what I'm gonna go with I look at--look

stands for integer we're going to go

through each one of these things and

we're going to go like this and we're

going to say in here we're going to give

it our array which was array and inside

our square bracket instead of getting at

the number we're going to say at symbol

which just means display all's actually

before I do that let me show you that by

itself so here I'm going to say the F of

the symbol so instead echoing a number

I'm saying echo app and now if you run

this you can see it prints out all of

our array all four items but we're going

to do here is we're going to say okay

for each item in this array so each one

of these put it in this variable and do

something with it so down here we're

going to do is going to say do this I'm

going to say done and that will loop

through that whatever we put in here it

will loop through so we're going to say

instead of echo array here I say echo

dollar signing I and what this is going

to do is it sets that loop it's going to

a dog cat that Mouse so I can do this

and run it and you can see it does that

and we can add things to our echo

command here I can say it is a and we do

this we'll go through it is a dog it is

a cat is it a bat it is a mouse and you

know if we wanted to put on you know a

little delay on there we can go sleep

and we can say point zero point two so

just less than a quarter of a second you

can see now we got on it prints out Ted

just displaying them all at once it just

okay um

so from here what we can also do I want

to show you let's say I wanted to say

little cat well obviously I couldn't

just go like this because if I do that

it's going to put little and cat at

different items so you'll want to put

those in quotation

and really it's a good idea to do that

even if you don't have to I like to put

quotations sometimes it's overkill but

sometimes it will help prevents errors

and mistakes like that so I don't want

some quotations around all of these only

the ones with spaces in them but let's

go ahead and run that again and you can

see that did not work let's try this

single quotes I think that will work

nope let's try one other thing nope

well actually I think I realized it now

let's see if I'm right about this I

should have put quotations around this

part so let's try this again let's go

ahead and remove all our tour tations

this well I guess I could have left

those so we're still going to see cat

little and cat on separate lines here

and now if I put quotations and I'll

explain why that is in a second there we

go now we got little cat so this will

divide this up and I got a couple

quotations around each one of these if I

want single or double um and and they

will be separate items in the array but

when we're doing the for loop the for

loop again is looking for spaces so if

we don't put quotations around it here

it's going to see even though it's get

passing one item the for loop is going

to see it as two different items so I

will mistake on my behalf

easy mistake to make again there are a

lot of situations where you don't need

quotations but in most cases I put them

all the time just to be sure and again

this is one time I didn't do it right

here and I should have and there was an

example of a mistake so let's go ahead

and run that again so again I don't need

rotations around the single words but

it's not a bad idea to have them there

so this is just a quick look at looping

through arrays with a for loop we're

going to build on this as

we go on in the future so thank you for

watching please think about subscribing

if you do like my videos also visit my

channel films by Chris calm that's Chris

decay there's a link in the description

there's also a link in description to my

patreon channel if you want to become a

supporter over there even a dollar a

month is helpful I appreciate it and as

always I hope that you have a great day