What is an Environment Variable?

John Lindquist
InstructorJohn Lindquist
Share this video with your friends

Social Share Links

Send Tweet
Published 3 years ago
Updated 3 years ago

Environment variables are a key part of software development as they can determine the behavior of how a process runs depending on where you run the process from. This lesson demonstrates running code from different terminals and loading in Environment variables in various ways to show what Environment variables are and how they are used in your code.

Instructor: [0:00] Anytime you write some code and then you execute that code from a runtime or compile the code as an app, this will start a process and then end a process. We can check the process, process ID. I'll keep this alive by doing a set interval that does nothing. It just runs every one second.

[0:23] Now, when I run this, you'll see I have a process ID of 93526. If you check all of your processes that are running, you'll see this one is 93526, and this is this Node process. Every process on your system is essentially running code, and every process on your system receives environment variables.

[0:45] We can check these environment variables. I'll go ahead and log process.env. I'll stop this process and then start it up again. You'll see a whole bunch of environment variables that are set, some coming from the terminal, some coming from things you may be testing, some you may have set yourself.

[1:04] The higher up you go in the list, the earlier those environment variables were set when the process started running. You'll see the very first ones that are set are things like user command mode, display path, and these environment variables that are set right at login.

[1:23] The reason I bring this up is that if I scroll all the way down, you'll see like VS code gitAskPass main as an environment variable. If I run this exact same code from my terminal, I'll run Node index, you won't see any of that stuff loaded in.

[1:38] There's no VS code environment when I launch it from here, because these environment variables are set from this shell. You'll also notice that a lot of the RVM stuff that I have in my terminal, all of these, are not set here.

[1:55] That has to do with iTerm loading specific scripts, which set specific environment variables. Depending on which tools you have installed, a lot of these version managers for tools will pollute your environment variables. You'll see a lot of environment variables set depending on your individual setup.

[2:12] If I were to take this script and send it over to you and you ran it, your environment variables will be completely different. If I were to take the script and run it on GitHub, that would be completely different.

[2:24] This env, the environment reflects where this process is running. You can check that directly from the terminal you're using itself. Type env and it will list everything from there, which should reflect accurately a process that is run from this terminal. It'll give you a good idea of what will be set from any process you launch from here.

[2:46] We can manually control the environment. If I went to set a message to, hello, and then run Node index, you'll see this message shows up right here as one of the most recent things that are set. It looks like this underscore is the only thing that's set after it.

[3:04] Once I start my process, it parses in the message. After that process starts, net command is run. The shell is remembering the command that you put in. You'll also be able to override, so let's override Yay right here. If I say that Yay is Yay instead of Aza, once I run this, you'll see that Yay is now Yay.

[3:30] Even though the terminal will parse in all these environment variables, you can still override them at any time. It's extremely common to use a project that stores environment variables in a file. We'll go ahead and add this in. We'll install .env, then we'll create a .env file.

[3:49] Now anything I type in here can be set to whatever. We have to take that one line of setup. You'll load in the .env package. We'll set that at the top. When I run Node index, I'm not overriding any environment variables through this command. I'm going to run this, and you'll see anything is set to whatever.

[4:11] You'll also see this is happening as the absolute last thing, even after the command is run. Then these are loaded in. While technically a .env file is more of a configuration file than it is an environment, it's more like mocking an environment, it's still a great way and a very, very common way to set environment variables, which will be used in the process that's launched.

[4:34] Once these variables are in here, they're set on the .env property of the process. We run this again and you'll see whatever shows up because we cleared that inside of this .env. If I remove this line so it doesn't load it in, I can run it again, you see it's undefined, and I can define anything here and run this again. You'll see we have it back.

[5:03] Depending on where you want your environment variable set, whether it's from that specific run of that process, or whether it's from every run of the process, or whether it's something you want to store in a configuration file on your system like in a .zshrc file, that's up to you on how you want to use and store those variables.

egghead
egghead
~ an hour ago

Member comments are a way for members to communicate, interact, and ask questions about a lesson.

The instructor or someone from the community might respond to your question Here are a few basic guidelines to commenting on egghead.io

Be on-Topic

Comments are for discussing a lesson. If you're having a general issue with the website functionality, please contact us at support@egghead.io.

Avoid meta-discussion

  • This was great!
  • This was horrible!
  • I didn't like this because it didn't match my skill level.
  • +1 It will likely be deleted as spam.

Code Problems?

Should be accompanied by code! Codesandbox or Stackblitz provide a way to share code and discuss it in context

Details and Context

Vague question? Vague answer. Any details and context you can provide will lure more interesting answers!

Markdown supported.
Become a member to join the discussionEnroll Today