Linux for Programmers #8 - Environment Variables On Linux

Tech With Tim · Beginner ·🛠️ AI Tools & Apps ·5y ago

Key Takeaways

The video discusses environment variables on Linux, including how to set, access, and modify them, using tools such as printenv, os.environ, and export, and covering concepts like secret tokens and sensitive information.

Full Transcript

[Music] hello everybody and welcome to another video in this linux or programmers tutorial series in this video we're going to be going over environment variables now environment variables are such an important thing that you need to know especially as a programmer and well i wanted to make an entire video dedicated to them it's not going to be that long but i really wanted to go through how they work and just show you guys these things because trust me you need to know these as a programmer and even on other operating systems as well in windows on mac you should understand environment variables and know how to use them so anyways let's dive in after a quick word from our sponsor which is lenode now as i've said many times now node is one of the best companies to use to host your website app or service in the cloud they have a great support team a bunch of awesome services and their one-click apps are just great when you want to spin something up quickly for example i've spun up a ton of minecraft servers by just going to lenode going on the one-click apps and then pressing on minecraft and there you go i have a minecraft server in the next what two to three minutes so anyways get a hundred dollar free credit for lenode by clicking the link in the description and remember to sign up to get access to the last five videos in this tutorial series you know where the link is it's in the description sign up here register with your name and email and you'll be notified when they are live anyways let's dive in here i want to start by explaining what an environment variable is then i'll show you how to print out environment variables how to access them in a different way and then how to change or set your own so an environment variable is quite simply a variable that is specific to an environment now i know that seems like a very generic explanation but i will kind of explain it to you with an example so a lot of times especially when you're a programmer you're working on some kind of project you're working on some kind of code base or whatever it may be and you have values that you need to know and that your program has to access but that you don't want to share with other people so let's say i have some like secret token or some password or login or something for a database and everyone who's like working on this type of project would have to have their own related to this like it's not a global thing that's shared between everyone else what you would do in that situation is you would say okay i'm going to set an environment variable that stores this secret token or this password or this piece of information i don't want to share with other people and then my program can access that environment variable now what that means is that when you go and you take this repository and you put it up on github and make it public for example if someone wants to use this repository they're not going to have access to your secret token they're not going to have access to your password or whatever it is that you're storing in this environment variable instead what you will tell them is that if you would like to use this program and you want it to work properly for you the first thing you need to do is set an environment variable you need to set in your environment whatever the variable name is that you decide equal to and then storing whatever information and then that way when this user downloads the code base so they pull that repository now they set their own environment variable the program will look at that environment variable which is different from yours and then it will use that to do whatever it needs to do so that's a very generic example but hopefully that gives you an idea of when you would use this most commonly you're storing like secret tokens or passwords or things that you don't want to share with other people but that are going to be continually used by your program in an environment variable now to print out the environment variables on your machine you can type env what this will do is print out all of them and you can see that if we have a look here we have pwd we have home we have all these other ones as well now notice though that the home environment variable this stands for our home folder is equal to slash root well why is it equal to slash root because our current environment we are logged in as the root user if we were signed in as a different user this would be different because we're in a different environment so that's the idea behind environment variables to store things specific to the environment that you're working in and then obviously when people are in different environments they can set their own environment variables that are specific to what they're doing so now i will show you how to print out specific values of environment variables notice i've created my own here called tim just for testing purposes and to print out the value of an environment variable you type print env then the name of the variable so here i printed the environment variable tim shows me that it is equal to yes now this is one way to access the value of an environment variable another way is to use this dollar sign so if you use the dollar sign and then you type the environment variable name say like user don't press run on this but that is how you would access this environment variable from a shell script or just from a program here in bash now i'll show you how we can use this dollar sign to access this so there's a command called echo and what echo does is it simply echoes to the console you can think of it literally as echo whatever's on the right hand side here so when i do this dollar sign this is telling bash that i want to access the user environment variable get whatever that value is and substitute that for this line which means echo will print out the value of user so if i do this we see that root prints out and that's how you access an environment variable with the dollar sign now this also means that when you define an environment variable you cannot preface it with a dollar sign so don't do that that's just bad don't have dollar signs in your environment variables i wouldn't imagine you guys would do that but just want to make that clear you're not supposed to do that so now let's talk about how we can actually set an environment variable and use environment variables so the first thing that i can do to create an environment variable is i can use this export command i can say export my variable name i'll just say var equals and then test and when i do this this will export a newly assigned environment variable to this environment so i'm going to say export var equals test now i can say print env var and notice it's equal to test now what i've just done is i've defined a temporary environment variable that is only stored for the current session so if i close this window right now and i log back in to this server this environment variable is going to be gone it's no longer going to be here and in fact let's just prove that so let's close the window let's go to putty let's sign back in we'll go as root we'll pass our pass phrase and now if i say clear and then i do something like print env what do we call this var notice that this variable has no value it's gone because that variable was only associated with our current session and now if i go env even my tim variable that one's gone as well so how do we set an environment variable that is constant is going to persist throughout our program well first of all if you want to change the value of an environment variable probably should have mentioned this you can say export and then whatever the environment variable name was so like you could change for example the home environment variable i wouldn't do that but you can so if i said like test equals one and then i say print env test and then i say export test equals two and then we have a look here print env test we're able to change that right so that is a way that you can change the environment variable you're kind of just overriding it but if you want to set one permanently you need to do something different now we will continue in one second after a quick word from our other sponsor which is algo expert algo expert is the best platform to use to prepare for your software engineering coding interviews as well as your system design interviews alongside over 115 coding interview questions algo expert also has another product called systems expert which is designed to teach you about large-scale systems how to design them and how to pass interviews related to them get started with algo expert and system expert today by hitting the link in the description and using the code tech with tim for a discount on the platform you actually need to modify a file that stores the persistent environment variables and this makes sense because you are just storing this environment variable it's attached to the current session so if you want it to be persistent you need to put it in a file it needs to actually you know be somewhere live on the system so i'm just going to clear the screen here now this file is located in our user's home directory so to access it what we can do is we can use nano this is a text editor we've seen a few times now we can say nano and then dot bash rc when we're in our home directory so when i do this notice it opens up this file that by default is hidden you have to actually access it to see it and inside here we can set environment variables so let me go through here i'll kind of scroll down you can see there's this is what's known as a bash script i'm not really going to talk about this but to define your own environment variable what you can do is you can go to any point in this file it doesn't really matter i mean like traditionally you would do this at the top of the file but you can do it anywhere you want and you would type the following line you would type export and then the variable name let's just say you know does this work and then is equal to thing like does this work if you want to have something that's uh more than one line or more than one word sorry not more than one line i just have to put inside a quotation mark so i'll put inside of quotation marks it'll say export does this work and then i'll just save this file so control s and then control x out of it and what i need to do now is i need to update my bash rc file or what's called source it so in order for these changes to take place what i can do is i can type the following command so i can type not s source but source and then dot bash rc and this will update the bashrc file or the environment variables for us and now if i print env i totally forgot what i called it let's just go back to env here my short-term memory loss let's see what we called it here looks like this one was called does this work okay yeah that was there you go so you can see that does this work is defined as an environment variable now what we just did by editing the dot bash rc file uh this is going to set the environment variables for this user so now every time we sign in as this user these environment variables will automatically be set so it's just going to run what's in that dot bash rc file it's going to see that export line and it's going to run that command and let's have a look at this again nano bash rc if we wanted another environment variable we'd simply do it on the next line below so it just runs this file it's going to see the export command it's going to declare this variable and that means it's just always going to be set for us but only for this user if i sign in as another user we're not going to see this now i don't need to save these changes i didn't really do anything but i'm going to show you now how you can set an environment variable globally so for any user that signs in now why would you do this i can't really come up with a great example off the top of my head but if you need to do it i'm going to show you how so to do this you do need to have access to the slash etc folder but if i go cd.ls notice there's this etc folder here we're going to cd into etc and then what we're going to do is we're going to say nano and then environment like that now there's no extension for the environment file all we do is say nano environment it's going to open something up like this and notice that the globally set environment variable here is path so we have a path variable you're probably familiar with this on something like windows or mac but we can define our own environment variables as well and we do the same thing that we would do when we're defining this inside of our bashrc we would simply say export and then we can just say something like global is equal to testing exclamation point then i can save this file i can write it out and what i need to do now is i need to source this file so i would say source and then in this case we just say environment that's going to update the environment file and now if i echo out this variable so echo dollar sign let's find the dollar sign and then global we can see that we get testing printing out so that's how you set a temporary environment variable just by using export right here as soon as you close the session it's going to be gone that's also how you set an environment variable for the user notice we updated the bashrc file from the home directory we were signed in as say tim we would do this again in the home directory and then to set something globally you go into slash etc and the environment file and you can set the environment variable there now the last thing i will show you how to do is to unset or remove an environment variable so if you want to remove an environment variable that you've exported in the in the session so when i say session again that means like if you sign out it's going to be gone what you can do is use the unset command so there's a command called unset i let's just make another environment variable let's just say export uh you know we can just say to be removed like that make this equal to you know buy sure let's add our quotation here and now if i said you know print env to be removed we see it has a value if i want to delete this environment variable i would say unset to be removed and this is going to remove the variable so now if i print env to be removed it is gone we have unset it now this unset command is only going to work for the session so this is not going to remove something from your bash rc file or from your environment file if you wanted to no longer have those environment variables you would have to go and re-edit those files and remove the lines that we added that's pretty much all i want to show you with environment variables this is not something hard it's not meant to be tricky this is just something that you need to know about and it's very useful especially as a programmer so anyways i hope you guys found this useful if you did make sure to leave a like subscribe to the channel and i will see you in another youtube video

Original Description

In this Linux for programmers tutorial, we're going to be discussing environment variables on Linux. Environment variables are such an important thing to know as a programmer. Environment variables aren't just important for Linux, but also on Windows and Mac, and you should understand how to use them. 💰 Get a FREE $100 Credit on Linode: https://promo.linode.com/twt/ 📚 Get early access to later videos in the series: https://event.on24.com/wcc/r/2952121/381131E9ADEA3203AEC3413E5A212643/1958924?partnerref=partneryoutube 💻 AlgoExpert is the coding interview prep platform that I used to ace my Microsoft and Shopify interviews. Check it out and get a discount on the platform using the code "techwithtim" https://algoexpert.io/techwithtim ⭐️ Timestamps ⭐️ 00:00 | Intro 01:21 | What is an environment variable 03:17 | Listing variables 03:38 | Printing specific variables 04:37 | echo command 05:20 | Creating variables 07:42 | Creating persistent user variables 10:20 | Creating persistent global variables 12:04 | Removing variables 📒 Playlist: https://www.youtube.com/watch?v=ebHX9c75H8I&list=PLzMcBGfZo4-nUIIMsz040W_X-03QH5c5h ◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️◼️ 💰 Courses & Merch 💰 💻 The Fundamentals of Programming w/ Python: https://tech-with-tim.teachable.com/p/the-fundamentals-of-programming-with-python 👕 Merchandise: https://teespring.com/stores/tech-with-tim-merch-shop 🔗 Social Medias 🔗 📸 Instagram: https://www.instagram.com/tech_with_tim 📱 Twitter: https://twitter.com/TechWithTimm ⭐ Discord: https://discord.gg/twt 📝 LinkedIn: https://www.linkedin.com/in/tim-ruscica-82631b179/ 🌎 Website: https://techwithtim.net 📂 GitHub: https://github.com/techwithtim 🔊 Podcast: https://anchor.fm/tech-with-tim 🎬 My YouTube Gear 🎬 🎥 Main Camera (EOS Canon 90D): https://amzn.to/3cY23y9 🎥 Secondary Camera (Panasonic Lumix G7): https://amzn.to/3fl2iEV 📹 Main Lens (EFS 24mm f/2.8): https://amzn.to/2Yuol5r 🕹 Tripod: https://amzn.to/3hpSprv 🎤 Main Microphon
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Tech With Tim · Tech With Tim · 0 of 60

← Previous Next →
1 A* Path Finding Algorithm(Visualization)
A* Path Finding Algorithm(Visualization)
Tech With Tim
2 Python Programming Tutorial #1 - Variables and Data Types
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
3 Python Programming Tutorial #2 - Basic Operators and Input
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
4 Python Programming Tutorial #3 - Conditions
Python Programming Tutorial #3 - Conditions
Tech With Tim
5 Python Programming Tutorial #4 - IF/ELIF/ELSE
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
6 Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
7 Python Programming Tutorial #6 - For Loops
Python Programming Tutorial #6 - For Loops
Tech With Tim
8 Python Programming Tutorial #7 - While Loops
Python Programming Tutorial #7 - While Loops
Tech With Tim
9 Python Programming Tutorial #8 - Lists and Tuples
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
10 Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
11 Python Programming Tutorial #10 - String Methods
Python Programming Tutorial #10 - String Methods
Tech With Tim
12 How to Overclock a NVIDIA GPU
How to Overclock a NVIDIA GPU
Tech With Tim
13 Python Programming Tutorial #11 - Slice Operator
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
14 Python Programming Tutorial #12 - Functions
Python Programming Tutorial #12 - Functions
Tech With Tim
15 Python Programming Tutorial #13 - How to Read a Text File
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
16 Python Programming Tutorial #14 - Writing to a Text File
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
17 Python Programming Tutorial #15 - Using .count() and .find()
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
18 Python Programming Tutorial #16 - Introduction to Modular Programming
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
19 Python Programming Tutorial #17 - Optional Parameters
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
20 Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
21 Python Programming Tutorial #19 - Global vs Local Variables
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
22 Python Programming Tutorial #20 - Classes and Objects
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
23 Cool VBS Script to Prank Your Friends!
Cool VBS Script to Prank Your Friends!
Tech With Tim
24 How to Overclock an AMD GPU
How to Overclock an AMD GPU
Tech With Tim
25 Best GPU'S For Mining Ethereum (2018)
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
26 Recursion and Memoization Tutorial Python
Recursion and Memoization Tutorial Python
Tech With Tim
27 Ethereum Mining Rig - Hardware Guide
Ethereum Mining Rig - Hardware Guide
Tech With Tim
28 Pygame Tutorial #1 - Basic Movement and Key Presses
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
29 How to Install Pygame (Windows 8/10)
How to Install Pygame (Windows 8/10)
Tech With Tim
30 How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
31 How to Mine Ethereum 2018 - WORKING (Super-Easy)
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
32 Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
33 Pygame Tutorial #2 - Jumping and Boundaries
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
34 Pygame Tutorial #3 - Character Animation & Sprites
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
35 Pygame Tutorial #4 - Optimization & OOP
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
36 OBS Studio Tutorial - Best OBS Settings
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
37 Linear Search Algorithm - Python Example and Code
Linear Search Algorithm - Python Example and Code
Tech With Tim
38 Make Any Mic Sound AMAZING! (WITH OBS)
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
39 Binary Search Algorithm - Python Example & Code
Binary Search Algorithm - Python Example & Code
Tech With Tim
40 Pygame Tutorial #5 - Projectiles
Pygame Tutorial #5 - Projectiles
Tech With Tim
41 Pygame Game - Mini Golf
Pygame Game - Mini Golf
Tech With Tim
42 Pygame Tutorial - Projectile Motion (Part 1)
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
43 Pygame Tutorial - Projectile Motion (Part 2)
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
44 Pygame Tutorial #6 - Enemies
Pygame Tutorial #6 - Enemies
Tech With Tim
45 Pygame Tutorial #7 - Collision and Hit Boxes
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
46 Pygame Tutorial #8 - Scoring and Health Bars
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
47 Cloud Mining vs. Hardware Mining - 2018
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
48 How to Install Pygame on Mac OSX (Fast-Simple)
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
49 Pygame Tutorial #9 - Sound Effects, Music & More Collision
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
50 Pygame Tutorial #10 - Finishing Touches & Next Steps
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
51 How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
52 How to Create a Button in Pygame [CODE IN DESCRIPTION]
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
53 Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
54 Pygame Side-Scroller Tutorial #2 - Random Object Generation
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
55 Pygame Side-Scroller Tutorial #3 - Collision
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
56 Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
57 How to Create A Message Box in Python - Tkinter
How to Create A Message Box in Python - Tkinter
Tech With Tim
58 Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
59 How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
60 Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim

This video teaches programmers how to work with environment variables on Linux, including setting, accessing, and modifying them, and provides hands-on examples using various tools and commands.

Key Takeaways
  1. Explain what an environment variable is
  2. Print out environment variables using printenv
  3. Access environment variables in a different way using os.environ
  4. Change or set your own environment variables using export
  5. Edit the bashrc file to set environment variables for a user
  6. Edit the /etc/environment file to set environment variables globally
💡 Environment variables are session-specific and can be changed with the export command, and to set a persistent environment variable, modify the .bashrc file in the user's home directory

Related Reads

Chapters (9)

| Intro
1:21 | What is an environment variable
3:17 | Listing variables
3:38 | Printing specific variables
4:37 | echo command
5:20 | Creating variables
7:42 | Creating persistent user variables
10:20 | Creating persistent global variables
12:04 | Removing variables
Up next
How AI Is Transforming Analytics in Tableau Cloud & Server
Salesforce Product Center
Watch →