 Hello and welcome today. We're going to be adding a list of numbers in our shell. Why might you want to do this? Maybe you have a list of numbers. It could be a spreadsheet you have that has a column of numbers and you want to add them together. But you don't want to open up a spreadsheet application and do it manually. Maybe it's like you're pulling down different spreadsheets and you want to script that goes through these automatically. Or maybe you have a bunch of pay stubs in PDF files and you're going to pull those numbers out and you want to add them together. Just some ideas on what you're going to do, but we're just going to generate a bunch of numbers today and we're going to add them up in our shell in a few different ways. So let's have a look at that. So first thing we want to do is if I just was to echo dollar sign capital random, it's going to give me a random number. Now, also if I was to echo and inside dollar sign double parentheses, I can say something like 8 plus 2 equals 10. If I say plus 20 equals 28. So we know we can add numbers that way. So I could also just say dollar sign random and I should get a random number and add 20 to it. And that's the number that I get right there. So let's get a range of numbers. So right now it's giving us a random number. I'm not sure what the max is, but let's say we want to get a number that is between 1 and 99. Well, I can do this command. I can say echo and I can say dollar sign and then double parentheses plus 1 equals random percent 99. And that will give us a random number between 1 and 99 every time we run it. So now that we have that, what we can do is let's create a long list. So every time we run that we get one, but let's run it 20 times. So I can put that in a for loop. I can say for 1 through 20. And that's not doing anything with our number over here other than just saying do this 20 times. And we're going to echo out that number and we're going to get a list of 20 numbers randomly each time we run it. Let's go ahead and take that command and then redirect it into a file. We'll just call it list.txt. So now we've generated that number. I can cat out list.txt and you can see it's a list of numbers. Now we want to add those together. And again, we know that if we echo and say double parentheses, we can get a bunch of numbers. We can say 25 plus 10 plus 20 plus 5 plus 5. It's going to give us the answer. So what we need to do is take our list file and put it so that we can get that plus sign. So there's different ways you can go about doing that. But what I'm going to do is this. I'm going to say I'm going to cat out that file, print out what's on that file. I'm going to replace all new line characters, the plus symbol. But then I'm going to take that last one because what that's going to do, that's going to give us, if I do that, it's going to give us our numbers all added together, but it's going to have this tailing plus sign. So what I would do is at this point, go back to what I had before, copy and pasting stuff so you don't have to sit here and watch me type it out. I'm going to say said and I'm going to say find the last plus sign and just make a new line character after that. It just gets rid of that last plus sign. So if we do that, we can take that and wrap it in our parentheses. So I can say echo dollar sign, double parentheses, and I get an error. Why did I get an error? I'm not really sure what I do wrong. Oh, okay. Sorry. I'm running command here. So I have to put it inside double parentheses again. See, that's why I should have copied and pasted. So this is saying, remove that, remove that. There we go. Now I'm pretty sure I have what I need. I'm running a command. That's what this dollar sign parentheses means. We're cat catting out the files out the file for each new line. We're going to replace the new line with a plus sign, remove the last one. But then the outside double parentheses and dollar signs going to add all that together. And we get a number and we should get that same number every time because we're listing the same file. So we have that. But what about decimals? Okay. So I can generate decimal numbers. How am I going to do that? This is probably a better way to do it. What I'm going to do is I'm going to say echo a number between 1 and 99, print a dot, and then echo a number between 1 and 99. That's just how I'm doing it for this tutorial. So every time I do that, I should get a decimal number, a float, if you will. So I can do that same thing and I can dump that into that text file. So now if I was to cat out that list file, you can see that I have all these decimal numbers. And if I was to try to use our command from before like so, it's going to work. So again, we're listing out the file, replacing all the new lines with plus signs, removing the last plus sign, then adding it all up. That works. I get a little bit of a funky number here at the end. So that's one issue. But also I'm in Z shell right now. If I was to run that same command in bash, it doesn't work. It doesn't like me doing that. It doesn't like all those decimals. So let me go back to Z shell. So we're going to do something that doesn't matter whether you're in Z shell or bash or fish or whatever. Instead of using the built-in echo double parentheses, we're going to pipe the information into BC. BC is a separate command. Pretty sure it stands for bash calculator. And if you're going to do more advanced math, it's a better way to do it. So we're going to do the same thing as before. So I'm going to say cat our file, replace all new line characters with plus signs, and then replace just remove that last plus sign. And we're going to say pipe that into BC. And we get a number without that extra tailing decimal. So not only is it not failing when we have floats, but you don't get that anomaly there at the end. There's videos out there on why that happens. BC is just going to do a better job of adding up floats. Now that's kind of a long command, right? We're using cat and then TR and then said we can actually do it all in one command. Every time I run this, I should get the same number because I'm listing out the same file. Instead of saying cat TR new line said, you know, we can use the pace command. So the pace command, I can say this, I can say dash s dash d plus. So basically I'm replacing each new line with a plus sign. So that's doing that and doesn't cause the end detailing plus sign. So that's good. And I can run that same command. It's a bit shorter, not piping, not running multiple processes. I'm just going to say BC and it gives us that same number out there because we're still piping into BC. This is just a shorter way of doing. This is what I came up just with my knowledge off the top of my head. And then I looked up a better way to do it and like, oh, pace. I love the pace command, but I always forget to use it. Okay. Another option would be to use AUK. So we've used the built-in shell for bashing Z shell, which worked somewhat. Had a little issue with floats. But we can also cat out that file. And what we're going to say here is some plus dollar sign one print some. So basically this is just going to add up each line. AUK is going to take that input and we get our output there. So there's another option instead of using BC, AUK can add up those numbers like this. This is a little bit harder to remember than just BC, right? But you can always alias that to something else. Anyway, that is a few different options for adding up a long list of numbers, integers and floats. And this comes in handy. Like I said, when I get paid, I get paid. My pay stub is a PDF and I save all those. I can convert those texts. I could use grep and said to pull out what I get paid. And then I can add it up and see if the numbers match for the year, stuff like that. Or if you just have a spreadsheet that already has a column of numbers, you can pull out the numbers like that and add them up in these different ways. It's definitely something that I've had to do a number of times. So I thought I made these notes. Check out the link in the description of this video. I'll put a link to my notes that goes over all of this. Thank you for watching. As always, please visit FilmsByChris.com. That's Chris the K. There's a link in the description. If you do enjoy my videos, and I hope that you do, think about supporting me on Patreon, patreon.com. There's a link to that in the description. I really do appreciate the supporters I have. It means a lot to me. A dollar or two a month is very helpful, especially if a bunch of people start doing it. And if you find my videos useful, it'd be great if you supported me. I feel that my videos go a little beyond what a lot of other people's videos do. So when you get to that intermediate to more advanced shell scripting, I feel like I go over that where it's harder to find videos like that, that people do consistently. And I try to put out videos consistently on lots of different topics. Thank you for watching FilmsByChris.com. That's Chris the K. Again, check out the link in the description. As always, I hope that you have a great day.