WEBVTT

00:00.140 --> 00:01.190
Hello friends.

00:01.190 --> 00:04.040
Welcome again to a new section of this course series.

00:04.040 --> 00:08.870
In this section we are going to see the fundamentals of Linux Shell scripting.

00:08.870 --> 00:14.420
This section is an update to this course for the year 2025.

00:14.450 --> 00:22.820
Okay, this section will be different as compared to other sections because this section consists of

00:22.820 --> 00:23.570
articles.

00:23.600 --> 00:32.030
Okay, all the fundamental topics are covered in the form of articles, and this video is really important

00:32.030 --> 00:36.260
for you to understand the basics of Linux shell scripting.

00:36.290 --> 00:36.770
Okay.

00:36.800 --> 00:42.740
There are two videos in which I am going to explain everything practically.

00:42.740 --> 00:49.880
Once you go through these videos, then understanding of the articles will become easier for you.

00:50.030 --> 00:53.270
Okay, so this is the first video.

00:53.270 --> 01:00.620
And in this video we are going to see how to declare variables, how to take user input and how to execute

01:00.620 --> 01:01.940
your script.

01:02.010 --> 01:02.400
Okay.

01:02.430 --> 01:03.630
So let's see.

01:04.920 --> 01:08.340
So as you can see here, these are the articles.

01:08.370 --> 01:09.030
Okay.

01:09.060 --> 01:15.210
In the first article we have covered what is Linux Shell scripting and its uses.

01:15.240 --> 01:15.870
Okay.

01:15.900 --> 01:21.690
When you read this article you will understand everything very easily.

01:21.690 --> 01:23.940
You don't need anyone's help.

01:23.970 --> 01:24.420
Okay.

01:24.450 --> 01:27.780
And this is applicable to all the articles.

01:27.780 --> 01:33.840
But before that, it is very important for you to understand the basics.

01:33.840 --> 01:38.910
Once you understand the basics, then you will understand the rest.

01:38.940 --> 01:39.570
Okay.

01:39.600 --> 01:46.350
And if you already know the computer programming, then you don't need to take these videos.

01:46.350 --> 01:50.070
You will understand every article very easily.

01:50.100 --> 01:55.110
Okay, so first of all let's understand what is a Linux shell script?

01:55.140 --> 02:00.540
Linux shell scripting refers to writing a series of commands for shell to execute.

02:00.570 --> 02:08.170
Means we write commands in a script file and we can execute those commands, okay.

02:08.200 --> 02:12.940
We can write multiple commands in a single shell script file.

02:12.970 --> 02:16.300
We don't need to type those commands again and again on the terminal.

02:16.330 --> 02:23.530
By writing multiple commands in a single shell script file, we can execute it again and again.

02:23.800 --> 02:28.000
Okay, after this, let's see the basic structure of Linux shell script.

02:28.000 --> 02:32.800
So as you can see here, this is the basic Linux shell script.

02:32.800 --> 02:35.890
It consists of different parts okay.

02:35.920 --> 02:38.860
At the beginning you need to include the shebang line.

02:38.890 --> 02:42.250
This shebang line consists of different parts.

02:42.250 --> 02:50.170
The first part is hash and negation symbol, and the next part is the path to the bash shell, which

02:50.170 --> 02:52.600
is present in the root bin directory.

02:52.630 --> 02:59.230
Okay, so here we want to use the bash shell okay for executing our script.

02:59.230 --> 03:02.440
It is also called as the born again shell okay.

03:02.470 --> 03:08.790
There are different types of shell present in the Linux, and you can use according to your choice.

03:08.820 --> 03:12.090
Okay, you gonna learn about it in the next article?

03:12.090 --> 03:17.520
Suppose I want to use the conch shell to execute this script.

03:17.520 --> 03:26.670
Then I just need to replace this bash with the ksh and our script will be executed with conch shell.

03:26.700 --> 03:32.670
Okay, so you need to include the shebang line according to the type of shell that you want to use in

03:32.670 --> 03:34.890
your script after this.

03:34.920 --> 03:37.830
This part here is called as the comments.

03:37.830 --> 03:42.990
So the comment is used to describe the purpose of your script or code.

03:42.990 --> 03:47.700
So it says that this is a simple shell script example.

03:47.760 --> 03:51.660
So it is explaining the purpose of this shell script.

03:51.660 --> 03:54.840
Then here it says define a variable.

03:54.840 --> 03:58.560
So here it shows the purpose of this line of code.

03:58.560 --> 04:02.160
So here variable is defined okay.

04:02.160 --> 04:05.470
So this is the purpose of this comment code.

04:05.470 --> 04:10.390
So comment are defined with this hash followed by the text.

04:10.420 --> 04:15.670
Okay so this is the basic structure which consists of shebang line then.

04:15.670 --> 04:16.450
Comments.

04:16.480 --> 04:18.430
Comments are not necessary.

04:18.430 --> 04:23.290
If you don't want to include the comment then you can exclude that part.

04:23.290 --> 04:28.330
Then your commands, then variables and then control structures.

04:28.450 --> 04:28.870
Okay.

04:28.900 --> 04:33.310
So this is the basic structure of a Linux shell script.

04:33.340 --> 04:33.850
Okay.

04:33.880 --> 04:37.090
After this let's see what are variables.

04:37.300 --> 04:37.810
Okay.

04:37.840 --> 04:40.240
Variables are used to store the data.

04:40.270 --> 04:41.080
Okay.

04:41.320 --> 04:45.370
If you know the programming then you will understand what are variables.

04:45.370 --> 04:48.160
So how to declare a variable.

04:48.160 --> 04:51.910
So to declare a variable you need to type the variable name.

04:51.910 --> 05:00.910
Then use assignment operator and assign the value here included inside the double quote okay as described

05:00.910 --> 05:01.600
here.

05:02.020 --> 05:07.160
And after declaring the variable, it is important for you to access the variable.

05:07.160 --> 05:14.870
So to access the variable, you need to add the dollar sign before your variable name that you declared.

05:14.900 --> 05:21.530
Okay, so as you can see in this code, a greeting variable is declared and assign the value.

05:21.560 --> 05:22.190
Hello.

05:22.220 --> 05:25.760
Okay so this is a string or characters.

05:25.760 --> 05:32.000
Then the next variable is name and assign the value Alice okay.

05:33.140 --> 05:40.580
And we are printing those values to the screen with the help of echo command okay.

05:40.610 --> 05:46.430
And we are accessing those values or these variables with the help of dollar symbol.

05:46.430 --> 05:54.200
You need to add the dollar symbol before your variable name and after that taking user input.

05:54.200 --> 05:59.450
So to take user input you need to use the command read okay.

05:59.480 --> 06:05.240
With read command, you can take any user input and then print it to the computer screen.

06:05.240 --> 06:07.800
Or you can perform your operation.

06:07.830 --> 06:08.190
Okay.

06:08.220 --> 06:11.640
It depends upon your script need.

06:11.670 --> 06:15.990
Okay, so we have learned that the basic structure of a Linux shell script.

06:16.020 --> 06:19.860
How to declare a variable and how to take user input.

06:19.890 --> 06:22.470
So this is just the theoretical part.

06:22.500 --> 06:24.720
So let's do it practically.

06:26.430 --> 06:29.430
So first of all let's create a script file.

06:29.430 --> 06:35.100
So I want to go with VI editor and type the name of the script.

06:35.100 --> 06:41.880
So I want to name it as let's say name dot bash.

06:41.880 --> 06:47.400
You need to add the same extension according to your shell that you want to use.

06:47.400 --> 06:50.340
So I want to use the bash shell.

06:50.340 --> 06:55.560
That's why I'm going to add the extension bash and hit enter.

06:55.590 --> 07:01.470
Okay, after getting inside let's get into insert mode by pressing I.

07:03.090 --> 07:06.300
So first of all you need to add the shebang line.

07:06.300 --> 07:08.960
So let's add the shebang line first.

07:11.540 --> 07:13.760
Bin forward.

07:13.760 --> 07:16.190
Slash bash.

07:16.700 --> 07:18.770
Okay, this is our shebang line.

07:18.890 --> 07:21.650
Okay, after this, let's declare our variable.

07:21.650 --> 07:25.910
So I want to declare a variable name.

07:26.000 --> 07:28.250
Variables are case sensitive.

07:28.280 --> 07:28.970
Okay.

07:29.090 --> 07:36.260
Once you declare the variable name as name like this, you cannot access the name variable with capital

07:36.290 --> 07:36.890
N.

07:38.090 --> 07:39.500
Like this.

07:39.920 --> 07:40.550
Okay.

07:40.550 --> 07:41.900
You cannot access this.

07:41.900 --> 07:49.070
You need to use the same spelling or the letter combination, whether it is small letter or capital

07:49.070 --> 07:49.790
letter.

07:50.060 --> 07:50.480
Okay.

07:50.480 --> 07:51.470
So.

07:53.870 --> 07:56.390
Let's assign a value here.

07:59.690 --> 08:01.010
This is my name.

08:01.160 --> 08:08.330
And after this I want to declare another variable address equal to.

08:08.340 --> 08:08.880
hear.

08:08.880 --> 08:12.660
I want to assign imaginary address.

08:15.870 --> 08:19.350
Host number 14 or 24.

08:22.170 --> 08:24.540
Mira Road, Pune.

08:25.110 --> 08:25.740
Okay.

08:26.310 --> 08:32.310
After this, let's print these values to the computer screen.

08:32.310 --> 08:35.040
So let's use the command echo.

08:35.070 --> 08:40.200
The purpose of echo command is to print the information to the computer screen.

08:40.230 --> 08:40.770
Okay.

08:40.770 --> 08:45.930
It prints a text or your variable data to the computer screen.

08:45.930 --> 08:50.760
So now, uh, let's use the dollar symbol.

08:50.790 --> 08:51.180
Okay.

08:51.210 --> 08:53.100
Now we are accessing the variables.

08:53.100 --> 08:55.440
So you need to use the dollar symbol.

08:55.440 --> 08:58.260
And then the variable name as it is.

08:58.290 --> 08:58.710
Okay.

08:58.740 --> 09:00.300
The way you declare the variable.

09:00.300 --> 09:04.650
So name and double quote okay.

09:05.070 --> 09:06.120
After this.

09:06.150 --> 09:09.490
Let's access the second Variable.

09:09.910 --> 09:16.900
Use dollar symbol and variable name like this.

09:18.760 --> 09:26.110
Now press Escape ctrl Shift colon wq and hit enter.

09:26.350 --> 09:29.800
Now our code is written to our script.

09:30.190 --> 09:31.660
Let's do ls.

09:31.660 --> 09:36.370
And as you can see name dot bash file is created.

09:38.710 --> 09:41.230
Now let's see the contents of our file.

09:41.260 --> 09:44.380
Cat name dot bash.

09:44.380 --> 09:47.350
And as you can see this is our data.

09:47.350 --> 09:51.790
So now it's time to execute our script file.

09:51.790 --> 09:56.410
So to execute our script file we need to make it executable.

09:56.410 --> 10:07.090
So let's make it executable with chmod add plus x and name dot bash and hit enter.

10:07.180 --> 10:09.670
Now our file is executable.

10:09.670 --> 10:17.300
And to execute the script, you need to use dot operator and forward slash and the file name.

10:17.300 --> 10:21.680
So let's add name dot bash and hit enter.

10:21.980 --> 10:25.490
Okay we got an error here at line number eight.

10:25.490 --> 10:27.650
So let's debug the script.

10:29.990 --> 10:31.640
Name dot bash.

10:34.130 --> 10:39.080
Okay here we need to add a space after echo command.

10:41.570 --> 10:44.150
Let's execute it again.

10:50.000 --> 10:52.430
Now it is executed successfully.

10:54.560 --> 10:55.940
Let's do it again.

10:55.940 --> 11:02.840
And as you can see it shows the name and address we have assigned to our variables.

11:03.230 --> 11:03.710
Okay.

11:03.710 --> 11:08.780
So I think you have understand how shell script work basically.

11:09.260 --> 11:12.390
So first we declare the variable name and address.

11:12.420 --> 11:18.930
Assign the values or data and print it to the computer screen with the help of echo command.

11:18.960 --> 11:27.390
So this is how you can declare a variable and assign data and print the data to the computer screen.

11:27.420 --> 11:31.590
Okay, after this let's see how to take the user input.

11:33.780 --> 11:39.720
So let's create another file v input dot dash.

11:41.640 --> 11:46.290
So in this example I want to take user input.

11:46.290 --> 11:50.010
So first of all include the shebang line.

11:50.640 --> 11:53.250
Bin bash.

11:54.300 --> 12:01.650
After this with equal command print the message to the computer screen.

12:02.460 --> 12:04.320
Enter your name.

12:04.500 --> 12:05.250
Okay.

12:05.520 --> 12:11.250
This time we're gonna get the user name from command line.

12:11.280 --> 12:11.670
Okay.

12:11.780 --> 12:16.580
so after this you need to type the command read.

12:16.610 --> 12:17.240
Read.

12:17.240 --> 12:20.690
Command will take the user input.

12:20.720 --> 12:21.080
Okay.

12:21.110 --> 12:22.820
With the help of a variable.

12:22.820 --> 12:25.580
So you need to declare a variable here.

12:25.670 --> 12:26.240
Okay.

12:26.240 --> 12:28.850
So the variable is name.

12:29.780 --> 12:35.120
After that display the entered name to the computer screen.

12:36.830 --> 12:37.130
Type.

12:37.130 --> 12:42.860
The message name is equal to our variable name.

12:42.860 --> 12:48.650
So use dollar symbol and the variable name and add the double quote.

12:48.770 --> 12:53.120
Okay after this press escape control shift colon.

12:53.570 --> 12:56.210
Now make the script executable.

12:56.210 --> 12:58.070
So first clear the screen.

12:58.220 --> 13:04.340
Let's see our file cat input dot bash.

13:04.370 --> 13:04.730
Okay.

13:04.760 --> 13:07.160
This is our data or our script code.

13:07.250 --> 13:09.440
Now make it executable.

13:10.760 --> 13:16.800
So just type the command Chmod plus x okay.

13:16.920 --> 13:19.260
And execute the code.

13:19.260 --> 13:23.730
So just type dot forward slash and hit enter.

13:24.120 --> 13:27.750
So as you can see the message is printed at the beginning.

13:27.780 --> 13:28.800
Enter your name.

13:28.800 --> 13:32.190
And now it is waiting for our input.

13:32.190 --> 13:38.250
So I'm going to enter my name here Prashant okay.

13:38.280 --> 13:44.580
Now this data is stored to the variable name with the help of read command.

13:44.580 --> 13:53.310
And when I hit enter, it prints that data to the computer screen with the help of echo command.

13:53.310 --> 13:58.320
So this is how you can take the user input okay.

13:58.350 --> 14:01.110
And print it to the computer screen.

14:01.140 --> 14:05.940
Or you can perform different operations with user input.

14:05.970 --> 14:09.840
It always depends on your script need.

14:09.840 --> 14:14.970
So this is how you can perform these basic operations inside Linux shell scripting.
