 Okay, today we're going to be looking at creating a basic timer like a stopwatch in bash So let's just go ahead and start let's create a variable. It's going to hold our time in it So we'll say let and putting let before a variable is just one of a few different ways To let the script know you shall know that this is an integer It's a number that we can add to and subtract from and we'll just call it x and we'll say x equals zero and Then we're going to want to loop. So we'll say while One which means go continuously until I stop it. We're going to say echo Dollar sign x and we'll sleep for one second and then we'll clear the screen. Let's not even do that Yeah, yeah, we'll clear the screen And then we will say Let x plus plus mean adding one to x each time done and there we go And that is how you create a basic timer stopwatch type application in bash. Thank you for watching and Just kidding. We're going to go a little more in depth than that. We're going to actually probably go over. It's probably It sounds something simple but there's a lot of issues with what we just did and a lot of improvements we can make and So there's actually probably gonna be two or three videos on this topic But yes, what I just did works you have a timer although fairly ugly and inaccurate And the reason I say inaccurate is let's go ahead and just take what we just created this little one-liner And I will make I'm gonna use Vim as my text editor again use whatever text editor you prefer I'm gonna open that up and what I'm going to do is I'm going to start off with my shebang line So always start off script with shebang line. We'll say bin bash So we're saying this is a bash script use the bash interpreter And then we will paste in not that that's the name of this video Wrong clipboard there we go. Okay, let's break this down on the different lines So that make it a little bit easier to see what we have going on So one of the biggest issues with what we just did that makes it very inaccurate is that here We're we're doing a loop. We're running a command printing something to the screen We're sleeping for one second then clearing the screen then adding to the number that we started with and The problem is even though we're sleeping for one second here These other commands take time to run and even if it's small amount think of it Let's say it's a hundredth of a second takes a hundredth a hundredth of a second For all three of those other commands to run That means every minute and a half Your tire is going to be close to a second off and that's if everything runs smoothly if a computer runs slow or something like that It's going to be completely off So we need to come up with a better solution For getting our seconds and the best way to do that is to use your system time because there's Most computers there's going to be a fairly accurate clock on there for at least for you know most common uses And if what we do is if we get the time that we're starting at we can always Subtract the current time from that if we're getting both in seconds. So that way We can then update our display every second But if the computer runs slow or other things It's still I mean the whole computer could freeze for five minutes once it starts going again It's going to start showing accurate time again. So let's go ahead and do that. So up here instead of X Let's also give this a better name. Let's just call it our start time And we're gonna put dollar sign parentheses here And I also like to put things in quotation marks whenever possible I'm gonna run the date command. So again the dollar sign parentheses is saying we're running command And we're gonna take the output of that command and put it into the variable in this case start time Date will give us our date, but if we do plus percent s What it's going to give us is let me go out to my shell here, and I'll say date plus percent s It's gonna give us this number here when I'm running this if I run it again You can see it's different because what this is giving us is what's called a Unix timecode or epoch And what it is is it's the number of seconds since midnight or the first second of the day 1970 I believe so January 1st 1970 and Most the time when you're doing programming this is how you're gonna want to store times. It's easily converted to other formats There's no Time zones there so you know calculate that later on no matter what time zone I'm in this is going to be the same Number until I convert to another time format and your computer should know what time zone you're in or you can tell it What time zone it is? But this type of time stamp is very very important and it also allows you to easily do math because you're just counting in seconds where If you're trying to do minutes seconds days, they're all different lengths of time Right, and we'll get more into that in either the next video or the video after that Go ahead clear the screen go back in our script so now we have whenever we start the script that is going to run and set our Current time in seconds to this variable and it is a number because we said let Let's go ahead and update our variable here. We'll say start time So printing start time But actually we don't want to print that we want to set that to a variable. So we're gonna say let I'm sorry. I'm getting ahead of myself Let current time so every time this loops we're going to get another Unix timestamp So we're gonna say again say date plus percent s So this is getting a time when the script starts boom we got that that's locked in That's not going to change unless we kill our script and start it over again But every time this loops, it's going to get the current time in seconds Now what we need to do is take that current time and subtract it from The start time so what we're going to do or subtract start time from the current time Let and we'll call this just seconds Equals and we're gonna say dollar sign Current time minus dollar sign start time so now we have seconds and We don't need that quotation mark there and then we'll sleep and Then we will do our echo Dollar time dollar sign seconds And we don't need this so this is our base code We're gonna add a few more things here in the end and actually let's go ahead and put this before we sleep Otherwise we're gonna have a blank screen for a few for one second at the beginning of our code So again the script starts we get the current time stamp then it continues to loop forever until we break it And what's gonna do is every time it loops it's going to check the current time in seconds then subtract The start time from the current time and give us those seconds And then we will output that to the screen and then wait one second. So again, we're sleeping one second here just So we're not just constantly displaying to the screen because we're only looking at seconds here So we only need to update once every second but even if our computer ran slow and it took to you know 1.1 seconds or like again if the whole computer froze and ran slow for 10 minutes Once it started running again, it would start displaying the correct time So I think we've got everything we needed right there Let's go ahead and make that executable with change mod plus X and our name or script You don't have to do that once and now we run our script and there we go So again, it seems very much like what we had before Except for it's a lot more accurate Especially as time goes on and it would only take a couple of minutes for our old way to get you know a second or two Add a sync which makes it is a big deal You know for a lot of stuff if you're you know micro seconds out of sync here and there It's not a big deal, you know, obviously there's sometimes you need very accurate time But this is a lot more accurate again if you're getting a second out of sync every minute and a half You know then by the end of an hour, you're gonna be close to a minute off That that's a big difference control C to get out of that now You notice I cleared out the the clear screen So at one point I had it was clear in the screen so it wasn't displaying down like that We're gonna do something a little different here and what we're gonna do is we're gonna say NE and Then what we're going to do after that is inside our quotations here at the beginning line. We're gonna say Backslash R. So what are we doing here? The end is saying don't print a new line at the end of the seconds here And the E is saying hey look for this this special character here And what that's saying is basically go back to the beginning of this line. So it's printing a line It's going to begin a line and printing the line and the next time it loops It's going to go back to the beginning that same line So now we can run that script and you can see it's printing it there and The difference between that is we're not clearing the whole screen every time So what we can do is we can come back here and at the beginning we can say Echo We can say that maybe something welcome to my timer and then down here instead of just seconds we can say time Something like that and then we will run our script. You can see it didn't clear the screen We can have a clear screen at the beginning of the script. I like doing that But now it's updating. It's not clearing the screen each time because maybe you don't want to clear the screen each time It's just reprinting that one line over and over again It's erasing it and we're actually it's backing up and then writing over it backing up writing over it So that's why I'm going to stop today in the next video We're going to go a little more in depth with this So I look forward to seeing you at the next video Please visit filmsbychrist.com. That's Chris the K There should be a link in the description as always you can go there and search through all my videos from both my channels and If you like my videos think about coming a support over at patreon.com where you get early access to videos and Other cool stuff. I do thank you for watching as always. I hope that you have a great day