 So we've been looking at drawing horizontal line breaks in our shell script, similar to an HTML where you can do a horizontal line break and it draws a line across the screen so you can divide things up nicely. We're going to actually throw it into a script today and create a function. So lots of times when you write shell scripts it does something simple and you go through basically procedural, but you can create functions in shell scripts and they're very useful sometimes and this is going to be a very simple example of that. There's basically a rule in programming is that you really shouldn't type anything more than once if you do, it should be thrown into a function. So let's go ahead and do that. I'm going to use Vim as my text editor, but use whatever text editor you like for scripting. I do recommend Vim if you don't have one that you already like. Check it out. We'll just call this line.sh. So in here we're going to start off, I'm going to make this a bash script. So again we can take commands that we did last time such as printf and I can say in quotation marks here percent and we can give it a number or like I said we can do the output of tput columns so we get the number of columns s backslash n so we're doing the width of our screen that number of characters in spaces and then as we said in the last video we'll use set instead of tr because we are going to get into some fancier stuff here. We're going to replace those with an equal sign in this particular case. So again if I save this and I make it executable, she'll only have to do that one time on each system. I can run it and it draws a line. Now if I go back into that script I can copy that and paste it a couple times and run it. I did it four times again because of the way we're doing this it doesn't matter how big our screen is it calculates how big our terminal is when that function is run and it will draw the lines accordingly. If I go back in here now I can say something like echo this is a line, I can come down here and say echo another line, I can say another line, another line, one smore, and again just as an example that should be good and we'll finish off with two lines. So I can run that and you can see it prints this is a line then it does a line, another line then it does a line, another line once more another line again then it does two lines after that. So it works, our script works but it's kind of ugly and we have all these printf functions which we do not need. So what I'm going to do is I'm going to delete most of these put one up here and what I'm going to do is I'm going to create a function. Now in shell scripts you can actually just give it the name of the function so in this case line this I don't have to have the word function here I could just write it like this I like putting the word function I just think it makes things clearer so I do. So now we have this function called line so I can come in here and all I have to do is type in line, line, anywhere I want to line, I can type in line, I can go line, line, line and now I can run that script and it draws those lines accordingly again even if I resize things I run that it knows how big my screen is. Let's go ahead and go back into there. So again we're creating a function here called line and any time we run the command just like any other command on your system or any other function on your system it knows to run what's in here. So now that we've done that we can actually clean up our code a little bit here make it a little bit easier to read I can say w equals and then here I can paste in this command. So basically that's saying run the command t puts column so we're getting that the number of the width of our screen and then here we can say w. Now I'm putting the w with the curly braces around I've done videos on that before when you write a variable in Bash when you're trying to call it you can do dollar sign that variable or you can put it in these curly braces why do you put in the curly braces because without the curly braces in this particular case it doesn't know where that variable ends and now it's going to be looking for dollar sign WS no such variable so that's not good. But if we put the curly braces around it it knows that that is our variable and we're saying number of spaces so now I'm coming here and I can run that command again that function or that program again the script we wrote and that function so something else we could do is also we can say here character and we can give it a character of equals and then we can in here say dollar sign char and so we get the same output there but now it's a little bit easier to see oh what's our character I can come in here and change that to a dash if I want I can run that now it's doing dashes I can come in here and I can say you know pound signs hashtags whatever you want to call them we can even because we're using set as we talked about in a previous video I can paste in there some Unicode skull and crossbones and now I get skull and crossbones lines there something else we could do is we could also say change this to dollar sign one and then if I want I can actually pass different characters so we'll do an asterisk there I think that would be okay I might have to put these in quotations we'll see in a moment yeah I think I'm definitely gonna put those in quotations let's go ahead and try running this yeah so some of them I do is something because they're special characters let's go back in here and it's probably not a bad idea to put them all in quotations even though it's not required for certain characters there we go did we do it for all them yep so so now instead of having a default character I can actually say line and give it a character and it will draw those lines depending on how you want to do this so yeah that's that's basically it writing in a function now gives us the ability to call that we also said default variable if we can say if one is empty then use a character in fact let's do that let's do it to do it why not I'm gonna say well we could we could do it if then statement or we can do it the short way because I know certain people out there like that and I've been trying to do it more this way too we can say if dollar sign one we can actually get the length of it or we can check the value of it I'll just check the value of it if it's empty if that's true then we will do char char equals and we'll give it a default of an equal sign and then we can say if not then we can do our char equals one and that should give us a default so let's go ahead and run our script again ran fine let's go back in here and for example this last one let's go ahead and get rid of that so we should get three lines of or two lines of pound and an equal sign at the end so there we go it works so now we've written a function you can tell some of this I didn't try out before I started this video so basically again what we're doing here is we're creating a function line anytime we call line it's going to look at the width of the screen and get that in W and then it's going to check did you pass it a character if you pass it a character then it's going to use that character as our line if you didn't pass it at anything it's going to use the equal sign so that's what it's doing there we've created a default if the user doesn't give one now keep in mind the way our script now we don't want to give it more than one character here so even if I gave it two pound signs or pound sign and the dollar sign they're gonna get rid of these lines here we run that we have a problem now is that we just double the length of our line so that's why we're getting two lines here rather than one is because we just doubled up on the number of characters we could write our function take that into account and only look at the first character that way if the user passes more than one character but maybe the user wants it to be a double line so it's all up to you as the designer of the program what you do anyway we are going to play with this a little bit more in the next video I hope you're enjoying this learning a bit and we're taking something simple as drawing a line and we're learning new skills functions you know and other things and getting them with the screen so I hope this is a good little lesson for you and hope you're enjoying it next we're going to get a little fancier at this it gets a little more graphical in the shell so keep on watching and hope that you have a great day