Check out a free preview of the full Complete Intro to Linux and the Command-Line course

The "Environments" Lesson is part of the full, Complete Intro to Linux and the Command-Line course featured in this preview video. Here's what you'd learn in this lesson:

Brian explains that environment variables are already set in Bash, and demonstrates how to add new environment variables using .bashrc.


Transcript from the "Environments" Lesson

>> So whether or not you realize that at any given time your bash shell actually has a lot of environmental variables that are already set. And if you wanna see those environment variables, if you just come in here and say printenv, this is actually going to print out literally all of the environmental variables that are set right now.

So you can see things I have in here, like my path, the mail client that I prefer to use, the user, the pwd, the home, the LS colors. This is all the colors that if you do ls-lash, like what color is used where. I don't know the syntax, you'd have to Google that.

And so what we can do now, we can say, we can actually use those environmental variables. So if I say echo user, it's gonna echo Ubuntu or echo- Hello, my name is user, right, it's gonna say, hello, my name is Ubuntu. So when it does this dollar sign user, it's interpreting that or it's gonna go out and find that variable and replace it.

So it's actually bash that's doing that, not echo. So it's actually done when the command is run. So let's say if in this particular case, I just wanted to change what user was, I can actually come in and say USER equals Brian. And I say echo, same command, right?

But now there's a different variable in there. It should say, hello my name is Brian. Now keep in mind this is actually gonna be for just this particular session, right? So as soon as I log out and log back in, it's gonna drop that variable, but it is sticky for the rest of the session.

So if I come back in here and say Ubuntu again, it's gonna say, hello, my name is Ubuntu. Or let's say we want to do one with greeting. So I can say, echo GREETING USER, how are you? So there's nothing here for greeting, so it doesn't know what to do about that.

So let's make a greeting equal to Hello. You don't have to put the quotes there in that particular case because there's no space there. So if I run that same thing again, what do you think is gonna happen? It's gonna say, hello Ubuntu cuz it's gonna be all one word cuz there's not enough spaces there.

Wait, no, it actually did space them out. Every time the sync surprises me with how the sentence ends up working, but in any case, so now I can actually come back here and say hello. I can change this to greetings. Run this again and it says greetings Ubuntu, how are you?

So these are like variables and the reason why these are like variables is because they are literally variables and so you can use these as variables. Now what happens if I wanna make some of these permanent, right, like let's say I have some sort of preferences here. The first thing you can do is you can sudo vi /etc/environment.

So anything that I put in here, let's say TEST_THING=blah blah. So now if I write and quit here, And I don't think it'll work on this one. What did I call it, test thing? But if I quit, And open shell again, Now, if I say echo TEST_THING, every time that my bash session starts up, it's going to reread from that.

But the interesting part of what I've done here is if I sudo su, or if I, yeah, switch users to Brian like I had in previous ones, and I say echo TEST_THING, looks like Brian has it as well, right? Because that etc/environment is shared by every single user on the computer, which is not necessarily what you want.

Usually when you are setting environment variables like that, you want it just for that particular user. This is similar with like, etc profile and etc bash RC like that's actually system wide resources. So you can actually set the bash RC, we'll talk about bash RC in a little bit, but you can actually set it for every user on the computer.

Most of time you just don't want to, you want to set it just for your user. So let's go ahead and see how to do that. We're actually going to do that right now with bash RC. So I want you to say vi tilde slash.bash_rc. Sorry, not underscore rc, that's not the one you want.

And also I don't want to be as Brian anymore. I wanna be back as Ubuntu, so I'm gonna say clear LS, and who am I? So you can see that I'm Ubuntu, I'm gonna say vi to my dot bash RC, not no underscore here. And you can see here there's actually quite a bit already in here.

Normally that's not necessarily gonna be the case when you're running Ubuntu but because this is multipass, multipass gives you a bunch of very good defaults that you can fall onto. So I'm just gonna go down here to the end, and I'm gonna add a new line here. And I'm gonna say export another variable equals something cool.

And I'm gonna write and quit from here. And now that's gonna be run every single time that I start up this particular user, right? But I can never remember what I called it. What did I put, I called it another variable. It's a great name, Brian. All right, so right now if I say echo, another variable, we're gonna have the same problem, That it's not gonna work.

And actually, that wouldn't work then anyway, because, well, it did. So that's what that export name right there means. It means I want to export this out to the general environment. What I can do is, I can say source -/ bashrc as well, that actually goes and reruns it cuz that bash RC is actually just a script that it runs at the beginning of every single session.

So now if I do this echo again, now it's there because that bash RC has been run. So we'll get more into bash RC at the very end of the course. But suffice to say it's where you're gonna do all your customizations, right? It's gonna be where you change your prompt, it's gonna where you set up scripts to use, there's a bunch of stuff you can do with all the colors that you wanna use, and then set up variables for just your user.

And that all goes in bash RC, you wanna put all that stuff in bash RC. Now, a thing that people always ask me is, what's the difference between bash RC and bash underscore profile? I had to look it up for this course cuz I can never remember, all I can always remember is use bash RC, don't use bash profile.

One's a relic of the past, which is the bash RC, so the bash RC is always run. And what I'll actually end up putting in the bash profile which is only run at the first login. So the first time you open bash interactively, that's when bash profiles run.

So in 100% of my bash profiles, so bash underscore profile, we'll open in VI, I just put this particular statement, if dash f Bash RC then, and I'll put source bash RC and FI. Well, oops, we'll get into what this actual syntax means with the if statement and all that kind of stuff and how to do more bash scripting.

But all this thing does is it says, if a bash RC exists, run that, otherwise do nothing. And that way you can be assured that every single time that your Bash session opens, that your Bash RC is being run. And that way you can put everything in Bash RC and not worry about Bash profile.

Again, if you needed something to only run on the first interactive log in shell, which I really cannot imagine what that is, but if you have that need, you can put it in Bash Profile. Otherwise, just put it in bash RC. Does that makes sense? So yeah, I keep all my environmental variables in bash RC.

Cool, does anyone have any questions about that? So every user has their own bash RC. You can see here I have mine right there. That was created for me, I didn't have to do anything for it. Brian would have their own bash RC, so every user has their own.

There is the one in, so if I say sudo vi /etc/bashrc If I put something here, this would be a bash RC for everybody. I really don't know why you would ever do that, but that's where you would put it.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now