How To Use Shell Environment Variables

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
one of the basic concepts that you need to grasp when you're working with the bad shield 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 best show 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 bash shell so let me switch over to my terminal and i'm going to 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 you know 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 going to say this is my name equals and i'm going to do derek so i'm going to set that variable and now anytime while i'm in this shell session if i do it 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 want to make that distinction that the global variables are always all caps and your local variables are always lower case 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 is 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 going to do a string of characters i'm going to do a sentence i'm going to say this is a line of text if i hit enter bash is going to complain it's going to say is command not found the reason is 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 open this new instance of bash what happens if i do it echo my name nothing nothing is return 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 the other shell and if i echo my string this is a line of text of course that's available in the 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 want to do is you want to 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 uh actually outputted i get derrick 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 want to 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 going to 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 show 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 going to be on their system you know your username is different than my username then somebody else's username so typically i will use the home variable in a path to somebody's home directory because i i don't know the absolute path as far as slash home slash what their actual user name is so the home variable is very useful i often use echo user if i want to echo somebody's username out in a script maybe i want to say hello to the user you know 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 0 and 32 767. so if i echo that a few times you know we always get a random number again from 0 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 1000 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 uh you guys and recently i did a video of where i was doing some scripting creating a deployment script for my dot files 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 you know is equal to zero and if it is what i want the script to do is actually i want it to you know 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 you know i can actually show you the script if i open this in vm it is in my git lab repos dt os and it is dt os dot sh what i'm going to do is i'm going to do a check here i'm going to 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 a interesting thing to know that the uid is available and that the root user on a linux system is always user id 0. 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 lsn.local 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 that particular shell program does it just gives me the time and date and it 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 uh mainly uh just stuff i output i'm actually not using this in x mode bar because x mobile 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 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 of directory colon name a directory colon so what you would do here is do path equals dollar sign path colon and then path to you know whatever directory you're adding hit enter and at least in this session that will 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 and now obviously you want to make the changes to the path permanent most of the time that's typically what most people want to do and you don't want to 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 going to be available to you typically you want to 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 uh slash etsy slash profile is one let me actually open that one in vm i'm not sure what is in my etsy slash profile so i've got some variables that i've uh set 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 now i don't have anything really going on in that file a 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 rid 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 the section that says if i i'm doing uh 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 exist applications is typically where app images place their files their executables and typically that is not in a shells 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 shield 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 bad 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 fck james mitchell paul scott west comey allen chuck curt david dilling gregory hicko michael john alexander peace arch and for tour polytech raver red prophet stephen and willie these guys they're the producers of this episode they're my highest tiered 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 alright guys peace
Info
Channel: DistroTube
Views: 32,443
Rating: undefined out of 5
Keywords: environment variables, linux environment variables, bash environment variables, shell scripting, bash shell, bash scripting, set environment variables, environment variable, bash shell variables, bash scripting tutorial, bash scripting tutorial for beginners, linux, gnu linux, command line, linux command line, linux terminal, linux tutorial, linux scripting, bash profile, shell path, distrotube
Id: 9ZpL8iDU7LY
Channel Id: undefined
Length: 15min 7sec (907 seconds)
Published: Thu Sep 09 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.