 One of the common things you'll do inside a Bash shell and especially inside a Bash script is run a test on something. Bash has a built-in function called test and what test does you give it an expression and basically you test if this expression is true then do this. If the expression is not true then do something else. So let me show you this in action. Let me switch over to my desktop. So let me open a terminal and let me switch over to the Bash shell because by default my terminal opens in the fish shell. Switch over to Bash and what I'm going to do is to run a test on something. First, let me assign a variable. I'm going to say num equals 10 and you notice when you assign a variable in Bash scripting num equals 10. There's no spaces around the equal. If you add spaces around it, it will not work. So we just assigned that variable the num variable. So let's run a test on num to see if it actually is set to 10. So I could do the test command here, right? So test and then dollar sign num because it's a variable. So you have to call it with the dollar sign test if num equals 10. If I hit enter, nothing happens. And the reason nothing happens is it doesn't actually return any output. The test command by default does not give you any output, whether the expression you gave it is true or whether the expression you gave it is false. What it does, it sends exit signals to the shell depending on whether it's true or false. So if it's true, it sends an exit signal of zero to the shell exit zero. You probably if you read a Bash script, you'll see exit zero, exit one a lot in Bash scripting exit zero means the command succeeded. Exit one means the command failed, but because it doesn't give us any output. What you want to do is test num equals 10 and and and and means if this first command succeeds and and do this right means you can proceed with the next thing we're going to tell you to do, which in this case, just echo the word true. And then I'm going to do a two pipe symbols that's a or or just echo false. So if the num variable does equal 10, then it's going to send an exit code of zero to the shell. So then it will do what follows and and echo true. If it's false, it will send an exit code of one to the shell, meaning this failed. So don't do this. So instead, do this. That's how this should work. So num does in fact equal 10 because we get a true. If I asked the shell if test num equals 11, you see I get false. Some other things I could have done, I could have asked, does num equal 10 instead of using a equal sign, I could have done dash eq for equal. And you can see it's true, or I could have asked it if it's not equal to 10. So dash and e for not equal 10, you can see that's false. One thing to note is when you're reading bash scripts, rarely are you going to see the test function explicitly written out like this using the test command. What most people will do in their bash scripting is they'll just write it this way. Instead of specifying test and then the expression, they'll just do two brackets and then two brackets at the end here. They'll just wrap the expression in these double brackets. So if num not equal 10 echo true or echo false, if I hit enter on that, you can see that is false because the num does in fact equal 10. So I can verify that and you see that time it returned true. Now, one thing I want to briefly mention because you will occasionally see these test statements with single brackets rather than double brackets. The single brackets is kind of an older version of this. It's POSIX compliant, but most modern shells have the ability to do the double brackets. And what this does, it gives you some extra functionality for these test statements. For example, the single brackets, they don't accept the and and or the double pipe inside the test expression there. You see, it works fine outside of the brackets. But if I had something inside the single brackets with and and or pipe pipe, it will fail. But the double brackets allow for that. The double brackets also allow for pattern matching, regular expression. It's just a better way to do this. And it's something that the bash shell handles quite well. And the bash shell, of course, is the default shell on pretty much every GNU slash Linux operating system. Even if it's not the default shell, bash is pretty much always installed on a Linux operating system because the double brackets are pretty much the standard. That's typically the way you'll see these going forward with this video. I'm just going to do the double brackets for these test statements from now on. We should also mention other than equals with eqne for not equals. You can also do dash LT for less than dash GT for greater than. If I say num greater than 10, you can see it echoes false because it's not greater than 10. It actually equals 10. If you're wondering about all the various little arguments that you can give to the test function, you can always run a man on test. And you can see test it checks file types and compares values. And by scroll down, you'll get a list of a lot of these arguments that you can pass. You can see if you're using integers, you can do equals to greater than or equals to greater than less than or equals to less than not equal. You even have this one here that's kind of neat. When you're comparing files, you can see file is older than file. So dash OT or dash NT file one is newer than file two. So you can do a lot of really neat tests on a files especially. And that's typically what you're doing in batch scripting. Typically when I run a test in a batch script, it's comparing one file to another or many times I'm running a test if this file exists, do this if this file doesn't exist, do something else or if is this file executable, which let's run that. So let's do a dash X. So this is a flag for the test command dash X. Is it executable and then give it a file name. For example, I know on my computer here, I could go dot config slash qtile slash auto start if I can type dot sh. That's a shell script that's that exists on my system. And I know it's executable. It's the auto start file for my qtile titling window manager. And I know for a fact it's it's executable. So let's test it. And if that is in fact true, then echo true or echo false. If I hit enter, I messed up. So I must have forgot some punctuation. Yeah, the ending double quotes. Let's run the test again. You can see it echoes false. So it actually says that that is not executable. That is interesting because I know for a fact that file is executable. Did I not type it correctly? Maybe it does not like the tilde. Maybe I needed to actually express it or write it out like that. Yeah, so it doesn't like the tilde character in that test expression. I had never noticed that before. So that file does exist and it is executable by the user, my DT user that's running in this shell. But let me test a file that I know does not have an executable flag turned on. So how about my config.py that's also in that same directory. That's a Python script, but it's not executable and false. Yeah, so that's exactly what we expected there. Another common thing you'll test for is you'll test if a string is zero characters in length, meaning it's an empty string, or if it's actually a non zero length of the string. So let me show you this in action. What you'll do is, well before we test something, let's assign a variable. So I'm going to do empty string equals and then just opening and closing double quotes. So it's an empty string, right? So let's test if that works. So I'm going to do a dash z for zero and then dollar sign empty string. So we're going to test this empty string variable, the length of the string. Is it zero? That's the dash z. And if it is, once again, echo true or echo false. And you can see in this case, it's true. Now if I ran the same test, I could also do it as dash n. What dash n is, is it a non zero value, meaning is it actually set to something? And you can see that returns false as expected. Now one other thing we could have done here, if I up arrow. So this returns true, right? Dash z for the zero value. If you wanted to, instead of doing dash n, you could also do the exclamation point at the beginning of the expression. So what we're saying is not, if this is not a zero value, you can see return as false. And if this is not a non zero value, return true. Now many times in shell scripting, instead of doing what I'm doing with running a test and an echo true or echo false, you'll see these written typically as if then else statements. So if I up arrow, basically what you would do is you would start at the beginning, if run the test on this, if that succeeds, you know, then do something in this case, I'll just leave it as echo true. And then semicolon here, else echo false. And then at the end, semicolon fi to end the if statement. So they're very similar. And they're just written slightly differently, typically in scripts, though you will see this as these if then else statements. Now one thing you can do is you can actually do the if part on multiple expressions if you want. So if this is true, or if this is true, for example, then do something, let me show you that in action. So we already have the empty string variable assigned, it's just an empty string, let's do non empty equals and let's give it a value, I'll give it a value of pizza, right? So now let me up arrow just so I don't have to write everything here. So let's do a test and let's run dash Z. So is this a zero value, the empty string. And then what I'm going to do is the double pipe so or, and then we'll run another test. And this test is going to be dash in for the non empty variable. So let me explain what we're doing here. So if this is true, or if this is true, so they don't both have to be true, only one of them has to be true, then echo true, else echo false. And in this case, it echoes true because actually both of these statements were true because this is a zero value, this is a non zero value, right? But if I up arrow and make both of them false, so in this case, I'll swap the flags here, non zero and zero, you can see it's false. But if I make one of them true, in this case, we'll test both of them for non zero values, you can see I get true. One last thing I want to mention, because it is such a common thing you'll do inside of bash script is you'll test if a file exists. So dash of if, and then the location to some file, for example, how about, well, let's see if my bash RC exists, because I'm in my home directory, there is a dot bash RC here in this directory. And if there is, I want you to echo true or echo false. And you can see the bash RC does exist. But do I have a dot bash aliases file? No, because I keep all my aliases in my bash RC rather than a separate file. So that is how you do a test on a file. And that's very useful in bash scripting, because sometimes you want to test if a file already exists. And if it doesn't, then create the file. But if it does, then you don't have to do anything. Sometimes you'll also want to test if a directory exists. For example, let's test for downloads. You can see I do have a downloads directory here inside my home directory. But let's test if I have a templates directory. I'm not sure if I have one. I actually do, even though I never use that particular directory. But let's test if I have a directory simply called test. You can see there is no directory called tests spelled with a capital T here in my home directory. So that's just a little bit of what you can do with the test built in in the bash shell. And again, I strongly recommend reading the man page for the test built in because it's actually a really neat little function that has a lot of real-world uses, especially when you get into bash scripting. You're going to find that test command extremely useful. Now before I go, I need to thank a few special people. I need to thank the producers of this episode. Gabe James, Matt Paul, Role West, Armored Dragon, Commander Ingrid, George Lee, Methos, Nate, Erion, Paul, Peace, Arch, and Fedora, Riella, Teets for Less, Red, Prophet, Rowland, Soul, Last, Retools, Devler, Wardgintu, Anabuntu, and Willi. These guys, they're my high-steered patrons over on Patreon without these guys. This quick look at the bash built in test would not have been possible. The show is also brought to you by each and every one of these fine ladies and gentlemen. All these names you're seeing on the screen right now, these are all my supporters over on Patreon. I don't have any corporate sponsors. I'm sponsored by you guys, the community. If you like my work and want to see more videos about Linux free and open source software, like The Bad Shield, subscribe to DistroTube over on Patreon. Peace, guys.