Hello friends.
Welcome again to a new section of this course series.
In this section we are going to see the fundamentals of Linux Shell scripting.
This section is an update to this course for the year 2025.
Okay, this section will be different as compared to other sections because this section consists of
articles.
Okay, all the fundamental topics are covered in the form of articles, and this video is really important
for you to understand the basics of Linux shell scripting.
Okay.
There are two videos in which I am going to explain everything practically.
Once you go through these videos, then understanding of the articles will become easier for you.
Okay, so this is the first video.
And in this video we are going to see how to declare variables, how to take user input and how to execute
your script.
Okay.
So let's see.
So as you can see here, these are the articles.
Okay.
In the first article we have covered what is Linux Shell scripting and its uses.
Okay.
When you read this article you will understand everything very easily.
You don't need anyone's help.
Okay.
And this is applicable to all the articles.
But before that, it is very important for you to understand the basics.
Once you understand the basics, then you will understand the rest.
Okay.
And if you already know the computer programming, then you don't need to take these videos.
You will understand every article very easily.
Okay, so first of all let's understand what is a Linux shell script?
Linux shell scripting refers to writing a series of commands for shell to execute.
Means we write commands in a script file and we can execute those commands, okay.
We can write multiple commands in a single shell script file.
We don't need to type those commands again and again on the terminal.
By writing multiple commands in a single shell script file, we can execute it again and again.
Okay, after this, let's see the basic structure of Linux shell script.
So as you can see here, this is the basic Linux shell script.
It consists of different parts okay.
At the beginning you need to include the shebang line.
This shebang line consists of different parts.
The first part is hash and negation symbol, and the next part is the path to the bash shell, which
is present in the root bin directory.
Okay, so here we want to use the bash shell okay for executing our script.
It is also called as the born again shell okay.
There are different types of shell present in the Linux, and you can use according to your choice.
Okay, you gonna learn about it in the next article?
Suppose I want to use the conch shell to execute this script.
Then I just need to replace this bash with the ksh and our script will be executed with conch shell.
Okay, so you need to include the shebang line according to the type of shell that you want to use in
your script after this.
This part here is called as the comments.
So the comment is used to describe the purpose of your script or code.
So it says that this is a simple shell script example.
So it is explaining the purpose of this shell script.
Then here it says define a variable.
So here it shows the purpose of this line of code.
So here variable is defined okay.
So this is the purpose of this comment code.
So comment are defined with this hash followed by the text.
Okay so this is the basic structure which consists of shebang line then.
Comments.
Comments are not necessary.
If you don't want to include the comment then you can exclude that part.
Then your commands, then variables and then control structures.
Okay.
So this is the basic structure of a Linux shell script.
Okay.
After this let's see what are variables.
Okay.
Variables are used to store the data.
Okay.
If you know the programming then you will understand what are variables.
So how to declare a variable.
So to declare a variable you need to type the variable name.
Then use assignment operator and assign the value here included inside the double quote okay as described
here.
And after declaring the variable, it is important for you to access the variable.
So to access the variable, you need to add the dollar sign before your variable name that you declared.
Okay, so as you can see in this code, a greeting variable is declared and assign the value.
Hello.
Okay so this is a string or characters.
Then the next variable is name and assign the value Alice okay.
And we are printing those values to the screen with the help of echo command okay.
And we are accessing those values or these variables with the help of dollar symbol.
You need to add the dollar symbol before your variable name and after that taking user input.
So to take user input you need to use the command read okay.
With read command, you can take any user input and then print it to the computer screen.
Or you can perform your operation.
Okay.
It depends upon your script need.
Okay, so we have learned that the basic structure of a Linux shell script.
How to declare a variable and how to take user input.
So this is just the theoretical part.
So let's do it practically.
So first of all let's create a script file.
So I want to go with VI editor and type the name of the script.
So I want to name it as let's say name dot bash.
You need to add the same extension according to your shell that you want to use.
So I want to use the bash shell.
That's why I'm going to add the extension bash and hit enter.
Okay, after getting inside let's get into insert mode by pressing I.
So first of all you need to add the shebang line.
So let's add the shebang line first.
Bin forward.
Slash bash.
Okay, this is our shebang line.
Okay, after this, let's declare our variable.
So I want to declare a variable name.
Variables are case sensitive.
Okay.
Once you declare the variable name as name like this, you cannot access the name variable with capital
N.
Like this.
Okay.
You cannot access this.
You need to use the same spelling or the letter combination, whether it is small letter or capital
letter.
Okay.
So.
Let's assign a value here.
This is my name.
And after this I want to declare another variable address equal to.
hear.
I want to assign imaginary address.
Host number 14 or 24.
Mira Road, Pune.
Okay.
After this, let's print these values to the computer screen.
So let's use the command echo.
The purpose of echo command is to print the information to the computer screen.
Okay.
It prints a text or your variable data to the computer screen.
So now, uh, let's use the dollar symbol.
Okay.
Now we are accessing the variables.
So you need to use the dollar symbol.
And then the variable name as it is.
Okay.
The way you declare the variable.
So name and double quote okay.
After this.
Let's access the second Variable.
Use dollar symbol and variable name like this.
Now press Escape ctrl Shift colon wq and hit enter.
Now our code is written to our script.
Let's do ls.
And as you can see name dot bash file is created.
Now let's see the contents of our file.
Cat name dot bash.
And as you can see this is our data.
So now it's time to execute our script file.
So to execute our script file we need to make it executable.
So let's make it executable with chmod add plus x and name dot bash and hit enter.
Now our file is executable.
And to execute the script, you need to use dot operator and forward slash and the file name.
So let's add name dot bash and hit enter.
Okay we got an error here at line number eight.
So let's debug the script.
Name dot bash.
Okay here we need to add a space after echo command.
Let's execute it again.
Now it is executed successfully.
Let's do it again.
And as you can see it shows the name and address we have assigned to our variables.
Okay.
So I think you have understand how shell script work basically.
So first we declare the variable name and address.
Assign the values or data and print it to the computer screen with the help of echo command.
So this is how you can declare a variable and assign data and print the data to the computer screen.
Okay, after this let's see how to take the user input.
So let's create another file v input dot dash.
So in this example I want to take user input.
So first of all include the shebang line.
Bin bash.
After this with equal command print the message to the computer screen.
Enter your name.
Okay.
This time we're gonna get the user name from command line.
Okay.
so after this you need to type the command read.
Read.
Command will take the user input.
Okay.
With the help of a variable.
So you need to declare a variable here.
Okay.
So the variable is name.
After that display the entered name to the computer screen.
Type.
The message name is equal to our variable name.
So use dollar symbol and the variable name and add the double quote.
Okay after this press escape control shift colon.
Now make the script executable.
So first clear the screen.
Let's see our file cat input dot bash.
Okay.
This is our data or our script code.
Now make it executable.
So just type the command Chmod plus x okay.
And execute the code.
So just type dot forward slash and hit enter.
So as you can see the message is printed at the beginning.
Enter your name.
And now it is waiting for our input.
So I'm going to enter my name here Prashant okay.
Now this data is stored to the variable name with the help of read command.
And when I hit enter, it prints that data to the computer screen with the help of echo command.
So this is how you can take the user input okay.
And print it to the computer screen.
Or you can perform different operations with user input.
It always depends on your script need.
So this is how you can perform these basic operations inside Linux shell scripting.