hello guys welcome to the next blog on shell scripting tutorial for beginners in the last blog we have learn how we can create our first script and how we can execute our first hello world script now in this blog we will talk about comments and variables so let's get started so first of all let's talk about comments because they are really easy to use and understand so first of all what are comments comments are the lines of code which are not executed by a script but are helpful to know some information about your scripts so how we can write comments we can write comments with a hash before a line of comments so whenever you start a line of code with hash then it will become a comment right so just write hash and then when I write this is a comment then this is a comment right and this will not be executed by a script but it will give you some information about your you know script and then the second way of writing the comment is like this also so you write your code or line of code and then after that start a hash and then this is also a comment right so this will also be your comment and it will also not be executed right now these are the two most common ways of using comments in your script there are some other ways but these are the most commonly used ways right now let's talk about variables so what are variables variables are the container which stores some data inside them right so whenever you define a variable this store some kind of data it can be a string data or any number on any other kind of data right now in shell there are or any UNIX system there are two types of variables one is the system variable and other are called the user defined variables okay so first is what are a system variable system variables are created and maintained by your Linux or UNIX operating system so these are the some predefined variable which are defined by your operating system and the standard convention is they are these are the variables which are defined in capital cases right so whenever you see some variables which are capital most likely they are the system variables now the other type of variables are user defined variables and these types of variables are created and maintained by the user like us right and these are type of variables are generally defined in lower cases but there is no you know strict rule that you need to write your variables in lower cases you can even write them in capital cases also so let's see what are what are some system variables which are we can use in scripts so for example I will write echo and then you write a dollar symbol before a variable and then there is a variable call for example bash and it will give you your bash shell name right and let's just save your script and let's run our script so I'm going to just run our script and what it gives this variable this variable gives us the name of the bash or shell right there are some more variables for example echo bash version so I'm going to just write bash version and then it will give me the bash version right and let me just copy it few times and then there is a one variable for example for your home then you use home this will give you the home directory and then there is variable called PWD present working directory it will give you whatever where present working directory you are working in right and there are some more variables in you know your operating systems but you just need to you know search name or just search the system variables right so I'm going to just term execute the script once again and what it gives but it gives me the bash name or shell name then it gives me the bash version this is the version of bash I'm using and then it gives me the home directory and then it gives me the present working directory which is desktop so I'm working on my desktop directory you can see here also right now let's talk about the user defined variables right so how we can define user defined so you can define user defined variables really simply by giving a name so for example I want to create a variable called name so I will just write the name here and then I will just give a equal symbol here or equal sign here and then the values so for example mark okay and how you can use this variable you can use this variable with a dollar symbol before it so I will just write echo and then I will just write dollar here and then the name of the variable so my variable name is name itself right so it will give me the name now with echo you can also write for example you can just write the name is and then the variable name right so this will give me the name whatever this name variable contains right and I will just execute my script and print the name is mark right so your variable name can be used with your echo you know string with a dollar symbol before it right so in the same way you can you know write some text here also and it will work fine so what we will write is for example I will write our shall name is this one right or our shell version name is something like something else so our shell version name is this right or for our home directory we can just write our home directory is home this is the system variable right or for the present working directory or current working with current working directory we can write something like this or current working directory is this right it will also work fine so let's execute our script let me clear the terminal and then I will just execute my script it gives me the output so first of all hello world and then our shell name is this or shell version name is this our directory is this home directory and then our current working directory is this one and then we have defined a user-defined variable which is this one right now let's define some other variables so let me define 10 and then this can be the variables right you can think of this as a variable and then I will name my variable as for example 10 well okay just random and then I will give you the value give it the value of 10 here right and let's print this 10 here so value and then I'm going to just give the name of the variable right and let's see what happens and I'm going to execute the script and gives me this kind of result which is not the result we are expecting right so the problem here is the variable name should not start with the number right so if your variable name starts with a number it will not be considered as a variable so what it prints here is this value here right it just disregards this kind of output because it's not considering this value as a variable right and if you start your variable name with a normal letter small or capital it will be your variable so let's print once again I print the correct value of the variables write your value can be or you can define a variable in capital also so something like well you something like this also it's totally fine and let's print it let me clear the terminal first of all and let's execute our script right and it executes the same result right so in this way we have learned how we can use comments and variables with our script so we have used the system variables also and we have used the user defined variables also so I hope you have enjoyed this blog bye for now
Post a Comment