hey guys welcome to the next blog on shell scripting tutorial for beginners in this blog and some of the next coming blogs I am going to show you how functions works in shell scripting so let's get started so like any other programming language bash also supports function though they have somewhat limited implementations right now first of all what is a function a function is a subroutine or you can say it's a code block that implements a set of operations so for any user it's like a black box right it has some name and it implements some functionality and then later user can use this function once or multiple times so let's see an example and let's see how we can use functions in shell scripting so there are two ways of using functions one is like this and this is the first look at notation and for this you use a keyword function and then you give a name to your function and then you execute some commands right the second notation for the function is like this so you directly give the name to your function and then you give these parentheses and then you use these curly brackets and here comes your commands right so like this or like this so you can use functions like this in this notation or in this notation okay so we are going to see both of the notations and we will see how we can use it so let's say we want to print hello world using a function right so let's do it with this one we can just say hello here or you can add say anything using function right so you can give any name to a function and then we can you know give some commands inside this function inside these curly brackets so I'm going to just give echo and
I am going to say hello here right and in the next function for example I want a function that whenever I call it it's going to quit the script right so let's say this function name is quit okay and what it does is it just calls this command which is going to exit our shell script right so let's use this function first of all this Hello function to print something so to use a function in a script you just call it okay so let's first run the script we have just declared these two functions right and whenever we run the script what happens nothing happens because we haven't called any function by its name so to call a function this is just the declaration of a function right so to call a function you just need to give the name of this function so for example I'm calling the function hello then I'm going to run the script is going to print hello right now let's call this function which is a quit function and I'm calling this quit function before hello function right and then let's say I'm just echoing something so I'm echoing for example some word whoo-hoo okay so let's run the script and let's see what happens so I'm going to just run the script and nothing happens because what we have done here is we have called a quit function first of all and quit function just creates the script and the chance of calling this or this does not come because this function is called first and it directly quits the script right let's see the sequence like this so first of all we call holo and then we call this squid function after the holo function and let's run the script and now holo is printed but this echo is not printed echo Fuu right because the sequence is something like this so first holo function is called and this is printed and then we call the squid so the script is exited and the chance of printing this does not come right then let's use this quit after the echo command and then once we run the script it prints hello from this function and then it prints fool from this command and then we call the quit function which exits the script okay so sequence of calling the function is important right so however you call the function in whatever sequence it will be called in that sequence so first of all we are calling the hello here then hello will be called and if you call you know quit at the top then quit will be called first and then the other function right so sequence is important and the function declaration can be done in any sequence right declaration can be done in any sequence but what matters is the calling of the function so on in whatever sequence you will call the function they will be called in that sequence right now second thing is how we can pass any argument to a function so there are some ways of passing some argument to our function and let's see how we can do it so call this function I have declared hello here right instead of hello I can just call for example print this is a print function right and then how it takes an argument or the argument can be used in the with the dollar symbol and then the number okay so one means the first argument then if we write 2 here then it means the second argument and then we write 3 here then it it means the third arguments for example dollar two is the second argument dollar 3 is the third argument and so on right so what we want now is what we want now is we want to call this print function right so we can just call this print function and how we can do or give our an argument you can just give the name whatever name or whatever argument you want to give after the name of the function so whenever you call this function and give this argument it goes here as the argument 1 + hello will be printed so let's see what happens so you can see a hollow will be printed and then who is printed and then we quit the application using this function now we can call a function multiple times so we can say hello world here and then you can see it prints hello and then world and then foo right so you can call it multiple times so hello world and then we can say again here right and then we run the script I says hello world again so you can call a function with multiple times with multiple arguments right and you can call the function in sequence ok let's give some other arguments here so you can give $2 to and then you can give dollar creature right and instead of calling it multiple times you can just care this world as a second argument and again as a third argument and then I'm going to just remove these callings and then run the script for example and it prints hello world again because we are capturing these argument using are these you know variables so variable one is for first argument variable two is for second argument and variable three is for the third argument right so this is the introduction to function function have more functionality than this simple operations and this we are going to see in the next blogs so I hope you've enjoyed this blog
Post a Comment