Bash script INTRODUCTION TO BEGINNERS Tutorial No. 1


hello guys welcome to the first Blog on bash shell scripting for beginners and in this Blog we will learn what is shell what is bash and what is shell scripting but before that let's talk about who can take this you know tutorial and what are the requirements of taking this tutorial so what I am using is I'm using an open to Linux operating system for this Blog utorial series so if you have any you know Linux operating system you will be good to go or if you have you know Mac OS or Windows 10 also you will be able to follow along this Blog tutorial because Mac OS or Windows 10 nowadays also supports bash shells okay so for this Blog series I will be using the Ubuntu operating system now if you don't know how to install open to operating system or if you don't know how some Linux commands works then google it or I'm will be starting from the beginning so it's not very complex if you are you know beginning you know from this Blog also you will be able to follow along so first of all what is shell a UNIX shell program interprets user commands which are either directly entered by the user or which can be read from a file called the shell script or shell program now it's important to note here is shell scripts are interpreted and not compiled so when you write a shell script it's interpreted by your operating system and you don't need to compile your she'll script in order to execute it so if you have you know editor you can write your shell script and you write awake and execute it without compiling this shell script right now there are different types of shells and we will be learning bash shell scripting okay so first of all open your terminal and on open - you can just press control + T to open the terminal or go here and search for the terminal right now in order to search what are the shell types which your operating system supports you just need to write cat and then just write et Cie and then write shells okay so cat /et c /l z' and press enter and you can see these are the different kind of shells which your system can support so for example my Ubuntu operating system support SH - bash and our bash and this bash is what we are going to learn right now what is sh sh stands for bourne shell which is the original shell still used on unix system or unix-like environments right so the first shell which was you know used is this shell now what is bash so Bash stands for bourne-again shell so after this shell this bourne-again shell was reinvented and it's basically based upon an improved version of this as such or Cheban shell and nowadays it's the standard GNU shell which is intuitive and flexible okay so it's more or less the standard shell which is used in most of the Linux operating system or you based operating system including Mac OS and nowadays Windows 10 also okay so these are the shell which are supported by your system now where Bash is located so to know where Bash is located you just need to write which bash ok and then press Enter and you will be able to see this output and this is the location where your Bash is located so now we have all the information about bash and let's create of our first hello world bash script so I'm going to clear the terminal so first of all what I'm going to do is I'm going to just navigate to my Desktop folder or directory and here I'm going to create a new file which is our shell script file ok so I'm going to use touch command to create the file and I'm going to name it as hello dot as such now in it's important to know here is dot SH extension is not necessary to execute your shell script so if you name it hello also it will work totally fine but for us whenever you want to open this shell script using any editor then it's you know useful to have this extension because your editor will be able to understand that this is a shell script file so it will give you a beautified version of this shell script using the editor ok so it's not necessary but a good practice to use this extension now touch command can be used to create a file so let me just press ENTER which is going to create this hello dot SH file you can see here right now let me give you a little bit more detail about this files whenever you create a file which with the touch command and whenever you do LS - L here and press enter you will see here your hello dot SH file have the you know rights here for read and write for you and read and write for the group and read only permission for the user and this we will see later what this means and how we can make this script executable little bit later but let's start writing some script so let's create a HelloWorld script with this shell script so what I am going to use is a editor called Visual Studio code but it's not necessary you can open this shell script using any other editor so for example on my Ubuntu operating system I can use G edit to write my script or as an editor or if you have them or Nano they will work fine but for me I like using Visual Studio code as my editor so I will be using Visual Studio code but it's not necessary you can use any other editor for you know writing script okay so for me I will just give code and dot this is for opening this directory using Visual Studio code but if you don't have Visual Studio code just double click on your shell script and will open with the default you know editor you have okay so my Visual Studio code is open now and you can see I have this shell script here and it's empty right so let's write some you know script here so the first line of script whenever you are writing a bash script its standard practice to use this hash here so first of all just write hash and then this bang here and this is called shebang sometime or hashbang okay and then write the location of your bash so we have used a witch bash to know the location and the location was /bin forward slash bash right so I will write this or whatever the location in your operating system you just need to write this location here so this has the interpreter to know that this is a bash shell script otherwise if you write here for example other shell for example as such then your interpreter will know that it's not bash it saw some other shells interpreter right so first line is that and then you can write entering your you know script so it works same as a shell so and you can give any shell script or any a shell command here so I'm going to use echo and then I will just write hello world here so hello world and echo command is used to print whatever you write in these double quotes okay so I will just save this and I'm going to minimize my visual studio code and now I'm going to execute this script so I have just write written two lines here right this first line for the location and second line is for the echo echoing hello world right so let's execute this so I'm going to just write point or dot and forward slash and the name of your shell script so just write the name of your shell script here okay and then press enter here and now it says permission denied and this is what I wanted to you know show you so because this file whenever you create using touch it has only read and write permission but not execute permission and what we are doing here is trying to execute this right so we need to change the permission of this file first and then execute it so how to change it using chmod command so just write CH mod and then just write plus X and then the name of the file which is hello dot SH which is going to change the permission of this file so I'm going to press ENTER and now just do LS - al once again and now you can see that you have read right by the user and then execute permission by you know by the group and read and execute permission by other users so let's try to execute it again so I'm going to just execute it and now it gave me hello world which is what we wanted to execute or which we wanted to print using our script so this was the first Blog on bash shell scripting I hope you enjoyed this Blog/Article in the furthe Blog we will see some more details about bash shell scripting so stay tuned for MORE blog/Article


Post a Comment

Previous Post Next Post

Recent in Technology News