 Okay, today we are going to be looking at colored text in your shell scripts, Bash in particular. And there's a number of ways you can do this, but we're going to be looking at using escape sequences or escape characters. So real quick, let's go ahead and just jump right in over here. I have some examples already in my history here. So what I'm going to go through is I'm going to go back up so you can see here I'm using the echo command, which you would normally do to display text. And then I'm giving it the dash E option, telling it to recognize escape characters, which is this backslash E, and then we have this bracket, a number and M. And 31 is for the color red. So if I do that, you can see that I get red text. If I was to go up, you can see that the number 34 is blue text. So if I hit enter, this is blue text. And if I go up again, another example would be 35, which is purple. And there we go, we have purple text. So now we have our text color. We can also change the background for the text, which is just changing the number. And conveniently, it's just 10 up for the same color of the background. For example, if we want a purple background instead of 35, it's going to be 45. And if I go up to the word we have blue instead of 34, we can do 44. And we have a blue background. And for red, we would go up to 41. Now there's a whole series of these colors and other options too, like underlining, blinking, reversing the colors, and even hidden text. So we're going to look at all of that. But how do you remember all these? Well, you can look them up, you can have a chart. I actually have a shortcut in my VIM settings that actually creates variables for all of these. So we're going to look at that real quick. But also I want to talk real quick about your color options. Now right here, I'm using XFCE. So we're giving these colors are going to be generated by your shell. In this particular case, bash. But if you're using a different terminal, you can set different colors. So I want to make that clear. So there is, you can tell it to be blue, but the terminal itself can be a different color. So for example, again, this is XFCE 4-terminal. And if I open another window here, you can see, let me scale the font so it's the same. I have the same shell, you can see right here, I can type. But you'll notice that my text over here is white, not green. That's because in XFCE terminal, I have set different colors. So for example, if I right click, so this is XFCE terminal, over here we have X-term. If I right click on this and go to preferences, I can go to colors. And you can see I have a text color option, a background option, and a tab activity color. But then I also have colors down here. If I hover over them, they'll tell me what it is. So here we have red. This is when we tell our shell script that we want red. In this particular terminal, this is the color that will show up. So I can actually make it blue. And when I do that, you'll notice that where we had the red as the background or the red as the text, it's now blue. But it didn't change over here because I don't have those settings for my X-term. So I just wanted to make that clear that the colors you set are set by the shell. But if you're using terminal settings, it may change it. That might be a little confusing if you don't really understand the concept. The differences between your command line, your shell, and your terminal. A terminal and a shell are two different things. And your terminal can decide what colors are used for certain things and also what fonts are used. So real quick, I'm going to go back into my preference here and adjust this back. So again, I hover over this. I see this is supposed to be red. And I'll pick whatever color I want for red. So I'll just pick a red color. And there it is. But that's why my text is green here. It's because I think green text looks cool on a black background. So those are my settings for my shell. Again, these are the same shells. You can see as I'm typing, I'm working in the same thing. Anyway, we're going to focus over here for this now. I am going to quickly create a shell script. I'm going to call it, actually I think I started creating one, so I'm going to remove it real quick. But I'm going to create something. I'm going to use Vim as my text setter. Use whatever text setter you prefer. I'm going to colors.sh. And real quick, I'm just going to create a bash script. So boom, boom, boom, bash. Give it the shebang line here. And then I'm going to say echo hello world. Make that font a little bit bigger so it's easier for you to read. Save that, make it executable, and execute it. We now have hello world. But again, we can use those escape codes. I can use it just as I did here using the dash E option and then giving it this number. But again, it's hard to remember all that. So in my Vim settings, which I need to do a whole tutorial on my Vim setup, which is all up on GitHub. It's under middle X1000, I'm sorry, GitLab, middle X1000, it's called my Vim setup. I am going to quickly here, when I'm in cert mode, I hit control T to bring up all my templates. I'm going to say bash color, and it creates all these variables for me. And I did not come up with this idea. Somebody else did, and I just ended up using it. And basically now I can use these variables to change the color of stuff. Capital letters are, if it's all capitals, that's the background color. If it's lowercase letters, it's the text color. So real quick, I come in here, and now let's say I want this hello world to be blue text. I just give it a variable, and since we're working with text that's touching it, we're going to put curly braces around it. So we're just going to say red, if I want that to be red now. When I execute that script, the text is now red. And we have to realize that also, if we don't undo it, that will continue throughout our script. So let's go back in here. If I was to put another line, echo, this is line two, that is going to be red as well, because we did not unset that color. The way we would do that in here is we would just go normal as our variable. Now that second line will go back to whatever your default text color is for me, it's green. So now if I want this to be blue, I just go like this. I'm going to say blue. And when I run that, we now have a red line and a blue line. Let me clear the screen to do that again, a red line and a blue line. And again, we have other options, and you can just look at them up here. But I can also do, let's say I want it to be blue text with a red background. Well now, the order doesn't matter, I'm just going to say capital red. And since it's capital, that means it's going to be the background color. So now I have red text, whatever your default background color is, and then I have blue text with red background. And if we look up here, and again, if you're not sure, you can go through. We have dim, if we want to dim the color, we can underline it, we can make it bold, we can strike through, so there's a line through it, so let's go ahead and do that one. I can add that to either one of these, so I'll add it to this one right here. Again, I'm calling this like a regular variable, but I'm using these curly braces because, let's say I didn't use the curly braces, and I was trying to say strict through, is actually what that says, strike through, strict through. See now it doesn't, now it thinks hello is part of the variable. So that's what these curly braces do, it's just saying this is the variable. So if you're familiar with variables in bash, and you've wondered why people use the curly braces, and it's just to make sure that it doesn't accidentally link to other text is the main use of that. But now I can run this, and you can see that it's, there's a line through it. Okay, other options we have, we can go, and again the order doesn't really matter, unless you're overriding one of the colors, but I should be able to say blink, I believe is the option, and there you go, now it's, the text is blinking. And I use this in script, so you might go, oh when will I use these colors? Like I have a script that I run that does a network scan, grabs all the MAC addresses for all the devices on my network, and then lists them, and any that are not known, because I have a list of known MAC addresses, at the bottom it will show all the unknown MAC addresses, along with their IP addresses, and they will flash red to get my attention. There's a, there's a device on your network that we don't recognize, so that's kind of important, so you just, you know, bring your eye to something. Let's look at some of the other options in here, we can go orange, blue, aqua, gray, so there's lots of color options, and not only do you have color options, but you also have the light color options, so you have blue and light blue, yellow and light yellow, green and light green. And then on the other options here are things like hidden, let's go ahead and look at hidden, so I'm just going to go echo, and down here we don't have to worry about any of these special characters, so up here again we do the dash E, telling you to look at these escape characters, and it's already taken care of that, the N is saying no new line, if you didn't do that then there would be a new line before each group of text that you have here. But I'm going to, what did I say, I was going to say hidden, I'm going to say this is hidden text. Now I didn't set normal up here, so I mean the hidden should override most of that, but you want to get in the habit of making sure you put normal back after each one of those. So I'm going to go ahead and run my script again, and you can see we have our red text with the line through it, we have our blue flashing text with the red background, and then we have hidden text, it's here, so you can see I can highlight it, and when I get to the end of it it jumps to the end of the line there, and it's there, but even though I highlight it I still can't see it, but I can highlight it like that and click to copy it, or paste it, and it has copied the text there. So that is how you can do hidden text if you're trying to hide text, which might be fun in certain like shell script games. Now let's go back in here and reverse will just reverse the colors, so whatever your shell colors are, your foreground, your text color, and your background color, I want to exit out of that. Let's go ahead and just say reverse on this. So now I run this and see my foreground color is set to green, and my background color is black, so it just did the reverse of that. Again, all this is just so you can make things stand out. If I was to open up a new shell here, so like this, you can see I utilize this, I have an ASCII, anytime I open up a new shell, it brings up some information here, it brings up the weather, it brings up certain things that I have, I need to update that credit card 5% back, and tells me what my current video is, and it's supposed to show me the likes and dislikes, and yeah, sometimes grabbing stuff from YouTube doesn't always work, and my uptime, but you see I kind of color coded some of that stuff. So how do I do that? So you can run it in a script, if we go back over here, I can run this, but now watch what happens if I run that script, but pipe it into a file, let's say text.text. Now, if I was to look at that text file, text.text, look at this, it's got the escape characters in here already. Now in a text editor, it would look like this, if I X add this and I just cat that file, it actually has all those settings stored in it, which can be useful. So you can put that into a file, and then cat out that file, and you don't have to worry about scripting out the colors because they're already saved in that file, the flashing, the lines through, the coloring, all that's all saved into that file. But that's also true, so I've mentioned before the program Chaffa, which I think is a great program, should be in your repositories. I know it is for Debbie and Sid, I'm not sure about Debbie and Stable or other distros, because I think it's fairly new in most distros. But I have a photo in here of my son. So there he is, you know, the coloring isn't perfect, but it made this nice text-based copy of the image. And Chaffa does a great job, and like if I was to lower the font, the text size on this and run it again, it adjusts automatically. So I can get somewhat of a, I don't want to say high resolution, but a higher resolution of that image. There's other programs out there that will do this, and I don't feel like they do as good of a job. Now, if I was to zoom in again, clear my screen, let's zoom in a little bit more. Again, I can run Chaffa on that picture. There it is. I can actually dump this into a text file. I can say pic.txt. And again, if I look at that pic.txt file, it's actually all the character information for that, saved into a text file. So now I can send that to somebody who's running a bashed shell, and even if they don't have Chaffa installed, they can cat out that text file, and it's going to display that on their screen like that. There's good things and bad things about this. First of all, it looks like this here, depending on your shell setup, it may not look exactly the same, right? For example, if I go to my TTY, it doesn't look as good as it does in my terminal emulator here. But also, the sizing is different than if you're actually running Chaffa. So again, for example, if I change the text size of my terminal and I run Chaffa, it will adjust the size of the picture. If I put a higher resolution into a text file, and someone runs it, so for example, let me lower my font size, let me go ahead and run Chaffa again without piping into a file like that. So that's a pretty good looking picture. If I was to zoom in enough, it's going to look like that. And that's what's going to happen if I was to dump this into a file and someone didn't have their font set to that size, it's going to look like this. So that's the drawback of embedding that into a file. But you can embed images into a script file or a text file without having that person needing Chaffa or whatever program installed, and you can display them on the screen. So there's pluses and minuses to that. I just wanted to quickly show that. So yeah, that's colors in your terminal quickly. I hope that you understood that. Again, a lot of these escape sequences are hard to remember, but that's why a list of these, and I'll try to put a link to a list of them in the description of this video. So basically, I'll put all these variables just like this in a paste bin, and then you can copy and paste them. And again, I'm going to do a video later on on my Vim setup video, which will use all the templates I create. So again, like for me, I can delete all this. When I'm editing a text file, I can hit Control-T for template, and it actually brings up all my templates, and then I can type in, and it uses fcf to search through them so I can do bash, header, background colors. I got some bootstraps. Even though I don't use Bootstrap anymore, I have some templates for that, and it will just put that right in there. So something I've been meaning to go over, and I'll do a video on eventually. But I do hope that you have found this video useful and learned a little bit about colors, mainly that you can use them. It's built into your shell, may display a little different depending on people's settings. If they have some really weird color schemes, it might be all messed up. But usually, people, if you're using a color scheme, it's going to be somewhat, if you have red, you're going to have some sort of red set to that, hopefully. But thank you for watching, and I hope that you found this useful, and I hope that you have a great day. Please visit FilmsByChris.com. That's Chris the K. There's a link in the description. And as always, I hope that you have a great day. Wait a second. Okay. Finished making this video, edited it, uploaded it, set it to go public, and then I realized there was something important that I wanted to go over. And that is that you can use this technique to also have colors and stuff that you WGET in Curl. Let's have a quick look at that. Okay, you may or may not know this, but if you try to go to my website using Curl or WGET or some sort of shell-based Web Interface, hopefully it's going to redirect you to a text-based menu instead of trying to just dump all the HTML out to your screen. So for example, I'm going to WGET, I'm going to go in quiet mode, I'm going to output to the screen here my website, and when I do that, I get this nice, easy-to-read-in-the-shell text. And it's color-coded, so you can see the URLs for the videos, the titles in a different color. You can see the title of that section with a purple background or a magenta background. You can also see example usage with Curl and WGET and how you can search through my videos. So how do I do this? Well, same way, if you generate it on the fly with a script or just put this information into a text file on a web server when you display that web file, just like you would see HTML on your screen, you, in this case, we're going to see colors. So let's go ahead and clear the screen. And for example, if I was to echo-e and in quotation, I was going to go backslash-e, bracket, we'll say 31M. And then I can say this is red, right? So that's what we've gone over so far. And I'm going to say new line, new color. I'll say 34, which is blue, I think. This is blue in color. Nope, I must have typed something wrong. Let's see here. Oh, I forgot the M after 34. There we go. So we have this is red. This is blue. If I was to dump this into a file, so I'm going to redirect it, in my case, to USR, WWW, or not USR, sorry, var, forward slash WWW, HTML. And I'll just call this, I have a file already called colors.text. I can put that in there. And if I was now to Wget and dump the output to the screen, let's say, HTTP colon colon, local host. And we're going to go file colors.text. And again, the extension doesn't really matter when we're using Wget like this. You can see the output is the same as if we were to cat out the file. And again, we can edit this file. So let me just go ahead and grab this and save Vim. I can go in here. And if I want to, I can copy some of this. I can copy that line, say this is still blue. Like I said, in color. And I'll do another one. I'll just say this. I don't know what color 33 is. This is another color. So again, I can cat out that file and you can see the colors just as I was to put in any other text file. But if I was to Wget it, as if it was running from a separate machine on a web server, I can just run through the commands like this, it outputs it the same. And even if I'm Wgetting it from a file, I can go like this. I can just put it into, we'll say, temp color page.text. I guess redirecting it like that's kind of silly. I should be using the output command, but whatever. Now I can cat out that file, color page text just like that. And again, another example, if I was to Wget my website again, Films by Chris, instead of outputting it to the terminal, if I was to put that, I'll just say fbk.txt for Films by Chris. I've now downloaded it and dumped that. And again, I can cat that out the file that I downloaded and it has all the colors. And if I was to edit it with Vim, you would see all of the characters that make up that code. So you can color code stuff on your website for the shell so that when someone accesses something on your website using Wget or Curl or some other text-based application to pull stuff down from the internet, it doesn't matter because it's plain text but it has those escape characters in there that allow for color and flashing and blinking and all that. So yeah, I wanted to go over that. I forgot to originally, so I'm adding on here at the end. This is, I find, very useful. Again, I set up my website specifically so that it's easy for people to use even at the shell and it doesn't take that much effort. I wish more websites did that but that's just how I roll. You can say thank you in the comments below if you ever access my site via the shell. So, yeah, colors because it's just a plain text file with those escape characters in there, it doesn't matter where the file is created or stored, if you're reading it as a text file, you'll get those colors in your shell as long as your shell supports colors. So, again, Filmsbychrist.com, that's Chris of the K, there's a link in the description. As always, I hope that you have a great day. Again, if you enjoy my videos, Patreon is a great way to support me. Link down in the description as well. Have a great day. Now you can roll the yellow sun lines and boxes.