 One of the basic concepts that you need to grasp when you're working with the Bastshell on Linux is environment variables. What are environment variables? They're a way to store data into memories so that you can then later use that data at the command line or through scripting. Typically you have two types of environment variables in the Bastshell. You have global variables and then you have local variables. The local variables are typically your user defined variables. You set them yourself, the global variables. You can also set those as well, but typically your system already has dozens of already set global variables that are available to you in your Bastshell. So let me switch over to my terminal and I'm gonna show you the command to get a list of all your current global variables. So if I do print env, that gives us a list of all the global variables that are currently defined on the system. Now if there was a specific global variable that you already knew the name of and you wanted to see what that has been defined as, you could do print env space and then name of the global variable, such as home, don't do the dollar sign, print env knows you're doing a variable after it so the dollar sign is not necessary. So if I do print env home, I get slash home slash dt because that's what the home global variable is currently defined at. Another simpler way that I typically do this is I just use the echo command to echo dollar sign home in this case because you do need the dollar sign for echo because echo by default, it will think this is a string of text unless you add the dollar sign in front of the home. Now let me show you how you can create a user defined local variable right here interactively at the command line. I can just create any variable name I want. I'm gonna say this is my name equals and I'm gonna do Derek. So I'm gonna set that variable and now anytime while I'm in this shell session if I do a echo my name, it will output Derek. And of course I can use the variable my name with any shell command. It doesn't have to be the echo command. One thing to notice that the local variable I set I purposely used lowercase letters in that because I want to make sure that that local variable is lowercase because all the global variables are all caps. And the reason you wanna make that distinction that the global variables are always all caps and your local variables are always lowercase is because you never want to be in a situation where you're defining a local variable and you use all caps and you actually overwrite one of the global variables because it shares the same name because you keep the global variables as all caps and your local variables as lowercase there's never a chance that you'll accidentally override a global variable or redefine a global variable. Because those global variables are very important because many things on your system depend on those things being set correctly. Now one thing to know when setting a variable Let me do this variable here. My string equals and I'm gonna do a string of characters. I'm gonna do a sentence. I'm gonna say this is a line of text. If I hit enter, bash is gonna complain. It's gonna say is command not found. The reason is because of this space. It assumes that this is the first command. My string equals this. We're setting that variable. And then the space means, hey, this next thing is part of a new command. Well, there's no such command is on the system, right? So obviously anytime you're doing a string of text that contains spaces, make sure you wrap it in either single quotes or double quotes. It really doesn't matter. But once you have that, and then of course I can now echo my string and you can see that was set properly. Now let's talk about child processes. So right now, of course, I'm in a bash session here. If I wanted to, I could open up a child session here. So this is a sub shell, right? So I've opened this new instance of bash. What happens if I do an echo my name? Nothing, nothing is returned if I echo my string. Well, of course I gotta spell it correctly, but still nothing is returned. It's because those locally defined variables that we set in the parent shell, they don't transfer over to these child shells. So there are not available to us. Let me exit out of this shell and go back to the other shell. And if I echo my string, this is a line of text. Of course that's available in the parent shell, the original shell we were in. Now, if you want these variables to be available in these child sub processes, what you wanna do is you wanna use the export command to make these global variables. So if I do export and I do my name and then let me type bash to go into again, a child shell here and I echo dollar sign my name. Now you see I get the variable actually output it. I get Derek returned. If I run the print ENV command one more time to get all of our global variables, we now should have my name equals Derek here, listed in the global variables. Now, if I was actually setting that as a global variable, I probably should have done that as all caps again, just for the naming convention, but I don't wanna keep my name equals Derek as a global variable. So what I could do, I could use the unset command. So I could unset my name. And now when I do print ENV, I don't see my name in the list anymore, just to double check it, I could echo my name one more time and nothing is returned. I'm gonna exit out of this sub shell and go back to the original shell that we were in. Now let's talk about some of the global variables that are often already defined on your system that you'll use all the time, either interactively in the shell or through your scripting. So some of the more common ones that I use all the time, of course, the home variable is very useful in scripting because when you're doing a script that involves somebody's home directory, many times you don't know what their username is gonna be on their system. Yo, your username is different than my username, than somebody else's username. So typically I will use the home variable in a path to somebody's home directory because I don't know the absolute path as far as slash home slash what their actual username is. So the home variable is very useful. I often use echo user. If I wanna echo somebody's username out in a script, maybe I wanna say hello to the user, something like that. That's how you would do that. Sometimes if you're unsure what language is set on a system, a good variable to know about is echo dollar sign lang and that will tell me English US, of course, is the language set on this system. Another really interesting one is echo dollar sign random. That gives us a random number between zero and 32,767. So if I echo that a few times, we always get a random number again from zero to 32,767. I'm not exactly sure why it's that specific range of numbers, but that's what that is. Another useful variable is the user ID. So echo UID, and that lets me know my UID is 1,000. That is what the DT user on my system, his ID number is. And this is very important because the user ID of the root user on your system is always zero. So every other user has a ID number other than zero. So you guys, recently I did a video of where I was doing some scripting, creating a deployment script for my dotfalls. And I don't want people to be able to run that script as root because it could be dangerous. So I do a check at the beginning of the script. I do a check to see if UID is equal to zero. And if it is what I want the script to do is actually I want it to echo a statement such as, hey, you are the root user, you can't be the root user and run this script. So I echo that statement. And then of course I run the command exit to do that. And I can actually show you the script. If I open this in Vim, it is in my GitLab repose DTOS and it is DTOS.sh. So what I'm gonna do is I'm gonna do a check here. I'm gonna do it if ID of the user equals zero, then echo this statement saying, hey, you can't be the root user and then exit one if that's the case. So that is all that is. So that is an interesting thing to know that the UID is available and that the root user on a Linux system is always user ID zero. Maybe the most common global variable that people often want to play around with is the path variable. So if I echo dollar sign, then path all caps. What this is, this is a list of all the directories on the system that are in the shells path. What this means is that this is where all the binaries, all the executable files on your system should live because this allows you to be able to execute those programs by just typing, you know, program name, right? Because if it's not in one of these directories, how you have to run your scripts and your programs, you actually have to do path to program. That is annoying. So make sure that you have all of your executables in one of these directories that are in your path or if they're in some directory that's not in the path, add that directory to the path. For example, I've got some shell scripts of my own in dot local slash bin in my home directory. If I did it LS in dot local slash bin, you see I've got these six shell scripts here. And typically how I would have to run them is actually run the full path, you know, from my home directory here, I could actually do a dot local slash bin slash clock, for example, and that's all that particular shell program does. It just gives me the time and date and then outputs that in, I believe I'm using that for my XMO bar up here. It's the reason I have that. Anyway, if I have this in the path, which I do, I have home dt dot local slash bin as part of the path, what I could do now, just type the word clock, right? Because it knows that's part of the path, so I don't have to do that absolute path to it anymore and I could do the same for anything in that directory, such as kernel, uptime, volume. Again, this is mainly just stuff I output. I'm actually not using this in XMO bar because XMO bar can actually output all of this stuff. It's actually got built-in commands for it. I'm using this, I think, for my DWM panel. Now, one of the things people often get confused about is how to add stuff to the path. So if you wanted to do something locally, of course, you could just do that right here in the shell. You could do path equals and then do path equals path, so path equals the current path, which is all of this stuff, right? And you can see all of these are colon separated values, name a directory colon, name a directory colon. So what you would do here is do path equals dollar sign path colon and then path to whatever directory you're adding. Hit enter and at least in this session, that new directory you added to the path will be available to you. Now, one neat trick is typically you're adding a directory to the path of a directory you're already in, such as your current working directory, so you could actually do path equals dollar sign path colon period, meaning add the directory I'm currently in to the path. Now, obviously, you wanna make the changes to the path permanent most of the time. That's typically what most people wanna do. And you don't wanna set this, of course, at the command line here because it's just temporary. When you restart your machine, restart the shell, it's not gonna be available to you. Typically, you wanna add path equals path colon and then the paths you're adding to some of the files on the system that get read on startup. So slash etsy slash profile is one. Let me actually open that one in VM. I'm not sure what is in my slash etsy slash profile. So I've got some variables that I've said here, some global variables that I'm using. But if I wanted to, I could actually add some path information here. Export path is here, but I'm not changing anything. So that's one of the files that if I wanted to, I could actually change my path in that. Another one is you've got several files in your home directory. If I did it LS, there should be several files that start with dot bash. Yes, I have dot bash underscore profile. That is the next one that gets read after slash etsy slash profile. So I'm not sure actually what is in my dot bash profile either. I typically don't play around in these very often. Yeah, I don't have anything really going on in that file. Bash login is another one. I don't know if I had a bash login file. No, I had bash underscore logout, but that's not the same file. But next in order that would get read is bash login. And then you also have a dot profile file in your home directory, typically. So if I did that, actually, I don't have a dot profile file. So that's another one that you could set your path in. Typically what I do, so I just set my path in my bash RC. So if I actually look through here for a second, let me find path and hit enter here. And I've got this section that says if I'm doing a directory checks here. If this directory exists, then add the dot bin directory to the path. That's what that statement is doing. If local bin, if that directory exists, add that to the path. And if home slash applications exists, applications is typically where app images place their files, their executables. And typically that is not in a shell's path. So if that is available on the system, I want you to add the applications directory to the path. So that's how I handle it. I just add this to my bash RC or my ZSHRC or whatever shell I happen to be using. A bash, of course, is typically the default shell on most Linux operating systems. It's definitely the default system shell on Arch Linux based operating systems like the one I'm running today. So that was just some of the basics as far as environment variables, as far as the bash shell, global variables, local variables. That's a very cursory introduction to them. But you need to know, especially some of the more important ones that you'll use all the time, interactively at the command line, especially you system administrators. And of course, those of you that are trying to get into bash scripting. Now, before I go, I need to thank a few special people. I need to thank the producers of the show, FC Gabe James Mitchell, Paul Scott, Wes Akami Allen, Chuck Kurt, David Dylan, Gregory, Heiko, Mike Erion, Alexander, Peace Arch, and Fator, Bollie Tech, Raver, Red Prophet, Steven and Willie, these guys. They're the producers of this episode. They're my highest-eared patrons over on Patreon. I also want to thank each and every one of these ladies and gentlemen. All these names you're seeing on the screen, all these fine ladies and gentlemen. These are all my supporters over on Patreon because I don't have any corporate sponsors. It's just me and you guys, the community. If you like my work and want to support me, subscribe to DistroTube over on Patreon. All right, guys, peace.