 Do you do any kind of shell scripting? Do you write POSIX compliant shell scripts or bash scripts? If you do, you really need to know about this program called shell check. Shell check is a linting utility. What is a linting utility? Well, linting refers to the automated checking of source code for programming errors and stylistic errors, things like that. So a linter, like shell check, what it does, it checks your script for errors. That's essentially all it does. For those of you that want to check out the source code for shell check, it is hosted over on GitHub. It is licensed under the GPL3 license, so it is free and open source software. And shell check is written in Haskell, which could be neat for those of you that are big fans of the Haskell programming language. Maybe you want some code to investigate, to hack on, maybe check out the source code for shell check for that. So let me show you guys shell check in action. So I'm going to switch over to my desktop here and let me launch a terminal and let me zoom in here and clear the screen here. And to run shell check, obviously, we need to run it on a shell script. And for you guys to see shell check in action, I really need a shell script that's got some errors in it. And right off hand, I don't know of a shell script sitting on my system that I know for sure is going to return some errors. So I'm just going to write one. I'm going to write a shell script and it's going to be full of errors. I'm going to write test.sh here. And I'm going to start writing a really bad shell script. So first of all, let's create a variable here. I'm going to say hello equals and I'm going to say hello comma because I'm going to have something else after hello comma here in a little bit. And then let's create a function. I'm going to do it this way. I know some people are going to wonder why I'm writing this function this way. That doesn't look like the normal way to write a function in shell scripting. Yes, I get that. But again, we're going to do things a little different here. So another thing I'm going to do is I'm going to do a local variable inside the function here and we'll do local my name and the my name variable. Let's, you know what, just let's just go grab my user. So let's get the user setting from the shell. And yes, you guys are probably already going crazy about some of the crazy errors that I'm making here. Let's go ahead and echo say, not say name. We haven't got a variable for say name. How about say hello? We're going to echo say hello and then space and then let's get my name because we set my name to be the user. And you know, that's fine. That's a rather horrible function, but it really doesn't matter. And then let me do something else here. This should trip a error too. Let's welcome message there. And let's do it and and and then also echo how are you today? And let's go ahead and grip something. Let's do something I know will probably fail and she'll check. I'm going to grip for A E I O and U followed by SH in my will have home slash dot bash RC. So as you'll grip my bash RC file for any words that are A S H E S H I S H O S H or U S H as part of the string. Now, let me go ahead and write that. I'm going to open up a new terminal. I'll zoom in here and let's actually execute test dot SH although I don't think it's going to do anything. There were way too many errors in that thing. Yeah, it didn't execute at all. So what we need to do is the problem. Why you need a program like shell check is because the shell itself often doesn't really give you much useful information as far as what the hell is wrong with this thing. Now there's a lot wrong with this part of the problem. But if we instead ran this through shell check, so let's do a shell check test dot SH. We have actually a lot of errors. I got to scroll back up to the top here. Many of you guys probably noticed the very first error. The very first thing of course you should do in any shell script is write a shebang. And that's the very first thing shell check does as it tells us, Hey, what are we trying to interpret here? Is this POSIX compliant shield scripting? Is this a bash script? And that's part of the problem is it doesn't know. Now obviously it needs to be a bash script for those of you that are wondering about this weird way I wrote that function. That actually works in bash. You can actually write functions that way. So we actually need to specify bash in the shebang. So let me do a crunch bang slash user slash bin slash ENV space bash for the shebang. Let me write that. And now I'm going to rerun shell check. And then the very first warning that was about the shebang that goes away. The next morning is complaining about the variable my name here. This one right here. That actually needs to be wrapped in double quotes to prevent globbing and word splitting, which is actually a common error you're going to find in shell check. And shell check really starts forcing you to use double quotes everywhere. Many people once you get used to using something like shell check, you just tend to put double quotes around everything. It seems like everything needs double quotes and shell scripting anyway. More often than not, it's right to put the double quotes than it is to leave the double quotes off of some of this stuff. So let me go ahead and fix the double quote issue here around my name. So that should fix that error. And the next error is about the welcome message here, which I purposely wrapped in these curly braces, knowing that that would fail. That actually doesn't need any brackets or parentheses or anything around it for calling upon that function. So let me get rid of that and that will fix that error. And then the next error is the grip that I added at the end. I knew it wouldn't like this is because the actual pattern that we're gripping here, we actually need to wrap that in quotes, because if we don't wrap the grip search pattern in quotes, what's going to happen is we could have some issues with globbing and word splitting again. So anytime you grip and then a pattern in a shell script, it does need to be wrapped in quotes. And let me go ahead and colon W here in Vim to write and let's run the shell check command one more time and see how many errors we get this time. All right, already we're having less errors. Let's see the welcome message is still complaining because it's referenced, but not assigned. And what I'm going to do is I'm going to leave that. I could try to fix that error, but I'm going to leave that for now. The next warning here is wrapping home also in double quotes, because of again, the file globbing and word splitting. So let's go ahead because that's an easy one to take care of. And write and run shell check one more time. And now we only have the one error message. And let's say this warning for some reason, you are okay with that warning that that error, that error technically being there, but you don't want shell check to always throw up a flag every time you run this through shell check. What you can do is you can tell shell check, hey, don't check for this particular error anymore. So what you could do is you can come in here and your script and let's make a comment and shell check space disable equals. And then you need the warning code. And for that one for the about the welcome message is SC 2154. So I just copy and paste that. Let's write. And then let's go ahead and run shell check one more time. And you see, I don't get any more errors. So that's how you work with shell check is you just go through and fix all the errors. And if you come to some errors that are not really errors, they're just warning messages and you can't really fix them because sometimes it'll throw up warnings that you can't really fix or sometimes fixing the problem actually breaks the script in some way. So how you fix that, you just go ahead and throw in a comment and just disable that particular warning for that line. And just to verify that shell check actually works, we actually should try to run the test shell script now that we've actually corrected it to where it should be a working script. Does it actually do anything? Yes, it says, how are you today? And it grips some stuff for my bash RC. But that's about it. I guess we could go ahead and fix the welcome message since I was showing you how to disable the warning message. Let's actually show you how this really works. And a bash script is when you're calling upon a function, you actually don't need the dollar sign. You don't need braces or any of that. So now let me write that. And now let's run test. And now it actually says hello, DT, how are you today? And then the grip stuff. And then if we wanted to run this through shell check one more time now, without disabling any of the warnings, you see nothing is returned. So that means this is valid shield scripting in the eyes of shell check. Now let me close that out. One thing I'll quickly do is I'm just going to run the man page for shell check. There are a few options and flags you could use with shell check. One of the more common ones is the dash s flag. So you could do shell check dash s and then name of shell. So sh bash ksh that way you give it a specific shell to check the source code against. You can also do the dash x flag that tells it to follow any kind of external sources. So if you sourced any other files in the script, it'll actually go out and check those as well. That's really the only two flags I think I ever use. There is a dash f flag for the format, you know, and how it lists out the errors by default. The format is TTY, which is just human readable plain text. GCC, which is the GCC compatible output. I don't know if that's something a human being could read or not. There's check style, which is XML format. And again, not very human readable. I don't know why you would do that. You could have it format the errors as a diff. I guess I could see the use for something like that. Anyway, there's a little bit you could do with shell script, but for the most part, it's a very simple to use program that is very easy to wrap your head around. The only real downside I will say to shell check is sometimes you do get false positives. You know, sometimes you will get something returned as an error. It's not really an error or you kind of intended it to be there. And that's where it comes in handy to be able to disable some of the warnings sometimes. Now before I go, I need to thank a few special people. I need to thank the producers of this episode, Epsy Dallas Gay, Blue Mitchell, Alan Akami, Archfee Tire 30, Choke David, the other David, Dylan Gregory, Lewis Paul, Polytech, Scott Stevens, Finn, Wes and Willie. These guys, they're my highest tier patrons over on Patreon. Without these guys, this episode you just watched would not have been possible. The show is also brought to you by each and every one of these ladies and gentlemen as well. All these names you're seeing on the screen right now. These are all my patrons over on Patreon because this show is sponsored by you guys, the community. If you'd like to support my work, look for DistroTube over on Patreon. All right, guys. Peace. We need a fish script to enter.