Transcript for:
Basic Math Functions in Bash

thank you when it comes to bash there's no shortage of things to do commands that are built in if statements and many of the other functions that we'll get into later in the series but in this video what we're going to do is take a look at math functions and actually this is going to be one of the easier videos in this series as well as the shortest video in this series there's a few different math functions that I would like to show you nothing that's going to take all that long so let's just go ahead and dive in and get it taken care of [Music] this time around we're going to take a look at some basic math functions and this will give you a better idea how bash handles different data types now granted math functions are not something you'll be using a lot but it is something that I want to go over and it will help you achieve well-rounded knowledge with bash so it's a good idea and when it comes to shells as well as programming languages math is actually handled differently for example in Python if you wanted to add 30 plus 10 you could do it like that now that's not going to work here in bash but you get the idea now if what you want to do is add some numbers together in bash you have to present the information in a way that bash understands what you're trying to do so doing it the python way that's not going to work here specifically in bash we have the evaluate expressions command which is abbreviated expr just like that that lets bash know that what you want to do is evaluate an expression so with our previous example 30 plus 10 that's actually going to work now as you can see it gave us the total of 40 which is correct so if I did it the python way it's not going to work but if I respect the way that bash prefers to receive that information then it works just fine and just like you see here we can add two numbers together by simply typing expr followed by the two numbers with a plus symbol in between them and subtraction actually works a very similar way as well we simply type expr and you could probably guess what I'm about to do and you're probably right 30 minus 10. I'll press enter and it gives us the total of 20. in addition to that what we could do is divide by changing the minus symbol to a forward slash just like that and as you can see 30 divided by 10 is 3 but you get the idea now when it comes to multiplication you might think and you'd be forgiven if you did think this that you could simply type 100 asterisk four but that actually isn't going to work and the reason why is because asterisk and Bash is a wild card it means well everything run this command against everything in the local directory that's why it's telling me that desktop is an unexpected argument because desktop is the first thing alphabetically that it ran across and since asterisk refers to everything it's going to run this against every directory that's not what we want but that's easily solved because what we could do is escape out the asterisk so backslash right there in front of the asterisk I'll press enter and now it works and sometimes escaping something out in bash like I'm doing right here there are definitely times where you'll need to do that so just keep that in mind if you get some sort of error like this or it's not quite interpreting things as you expect are you using a special character that has some other purpose well asterisk does have a special purpose in bash so that's why we weren't able to use it by itself in an expression now in the previous lesson we went over variables one thing I definitely want to show you in this video is that you can add variables together as well so for example I'll create a variable named my num1 I'm going to set that equal to 100 and as you can see it worked I declared the variable it's set to 100 it shows up as a hundred so far so good anyway what we can do is type expr and then the name of the variable then we can add a number to it and as you can see it behaves the same way as if we've manually typed out 100 where the variable is but we didn't have to type it out we were able to save 100 in the variable and then add 50 to it like you see right here similarly I'll create another variable and we will add the first variable to the second one just like that and as you can see it works just fine now this might not seem all that useful right now and to be honest it really isn't but this is just one of those things that we'll probably use later on if nothing else I wanted to show you how to evaluate expressions on the command line and now you know how to do that and this might be especially useful if someone is coming from a different shell or a different programming language now you know how to do math functions within Bash [Music] so there you go in this video we went over some basic math functions and this was also the shortest video in the entire series but you know what we're moving right along through the series and we're going to have a ton of fun and I can't wait to see you in the next episode and since that episode is actually already on the channel then you can go ahead and check that out anytime you're ready to do so in the meantime thank you so much for watching I really appreciate it [Music] thank you [Music]