Easy…it’s very simple! Environment and Shell variables are very easy to manage. You just have to know a few concepts.
What are Environment Variables?
They are just a chunk of memory in shell sessions, in the form of variables, used to aid programs. For example, imagine, you are running a program who needs to know the home directory of the user running it. In this case, the program/process could make use of the HOME variable, which is holding the home path of the users home directory.
This HOME variable is loaded during the loading of the shell, being only visible to that session and to their childs. As this variable is declared…others might be too, you just have to know how.
How Do I Know which Variables are Declared?
Before you set a new variable, you might want to know which variables are already in use in your session. For that, just type:
The env command (alone with no parameters), prints the environment variables declared in the current session.
The printenv command, followed by a variable name (optional), prints the value holded by the named variable.
How Do I set a new Variable?
To set a new variable, you only have to write the syntax:
It’s good practice to keep every declared variable capitalized.
The value can be anything you wish, from numbers to text. Keep in mind, though, that text should be used with quotations, because of spaces.
Lets give it a try. Type:
|FOO=’This is my first environment variable’|
Good. Now type:
The result should be:
|This is my first environment variable|
Now, according to what we’ve learned, if we type env, we should see the variable FOO in list right? Yes, but it wont. Why? Because it isn’t an environment variable yet. Wait….whaaaat??? Easy. Here’s where the next concept comes in: Shell Variables.
What are Shell Variables?
Shell variables are just like Environment Variables, but only visible to the active shell and not to their child. It’s just like a “private” variable. Sounds confusing? Trust me, it’s not. When you start experimenting you will see that it’s peace a cake.
So, how do i see Shell variables?
How do i set Shell variables?
You’ve just did it with FOO.
How do i turn a Shell variable into an Environment variable?
Now to clearify the diferences between Environment and Shell variables, lets try something:
1 – Declare a Shell variable, for example: VAR=12345.
2 – Now type set. You will see the variable in the list. Now type env. You wont see it in the list, because it’s not yet an environment variable.
3 – Now type bash, to open a child shell.
4 – Type set. You wont see VAR in the list, because it’s a shell variable (private variable) of the parent shell. Type env. You wont see it either, because it’s not an environment variable. Type exit, to go back to the parent shell.
5 – Type export VAR, to make VAR an environment variable (available to the current shell and all their childs).
6 – Type bash, to open a child shell.
7 – Type env. You will now see VAR, because it’s already an environment variable.
Okay, lets resume all this stuff. Environment variables in Linux are composed by two types:
- Environment Variables – available to parent and child shells of the current session
- Shell Variables – available only to the current shell, in a private way
One more thing. You can declare these in the standard bash login files. If you dont know what i’m talking about, just check out my article about this:
Bash Login Files Main Diferences.