In this lecture I want to talk about environment variables.
Environment variable is simply variable that is available during the entire session in the shell.
And you could use environment variables, for example, in shell scripts.
Also, environment variables like path are used by system itself.
If you want to get a list of all environment variables that were set, you could simply type command
env three letters and you'll see a set of key value pairs.
Here.
On the left you see name of the variable like term user terminal version and so on.
And after equal sign you see value for a specific variable.
For example, here you see variable name like terminal and its value is iterm2.
Here if you scroll a bit up, you'll see variable called LS colors and its value is set here.
Also here you see such variable as WD and it is set to slash root.
You see such variable as shell and it is set to bin bash.
Now I'm logged in via SSH to Docker container as root user.
Let's now try to open up new SSH session as Bogdan User recap that I have created a new user before
and if you have not done it, please create a new user with any username.
And here let's use again SSH command.
I'll use same port 2222.
It is exposed port for docker container and here will be username Bogdan at localhost.
Like this.
Let's enter password for Bogdan it is 123 in my case and I'm in.
Let's now enter in the command here and now I'll see a set of similar env variables.
Let's quickly go back to this tab where I have entered the same command for root user and let's compare
those outputs.
Here is output for root user and for example here you see environment variable user that is equal to
root.
But if I switch to this tab, I'll see that user is set to Bogdan And also here you see different SSH
connection variable.
Here in this case, you see port three, five, five, nine, eight.
And here in this case, you see another port, three, five, five, seven, eight.
Because here are two different sessions and actually same information you see here on this line as a
client.
If I go back to another tab where I have logged in directly to the container this tab and enter in the
variable, I will not see such variables as I see here as a client and as a connection.
And that proves that set of environment variables is created when specific user logs into the system.
And depending on the conditions, logging conditions, those sets will be different.
All right.
That is a set of environment variables.
Now, question how we could access any variable.
It is very simple.
We have done that before.
Let's clear terminal here and simply enter echo dollar sign path, for example.
And here I get value for path variable.
And now let's try to get other variable, let's say user dollar sign user, and I get value for another
variable user.
It is very simple and of course you could use variables in shell scripts, for example, in order to
access some values.
And also you could set additional variables if you want.
For that you need to use command export and after export will be name of new variable or existing variable.
And next comes equal sign and value for the variable.
For example, let's create new variable called my underscore new variable like this.
And let's set it equal to simply test like this.
Let's press enter.
And now let's list all variables.
And actually, if you want, you could use here grep and here will be my let's say new like this and
I'll get value for my new variable.
It is actually available also in this output.
Let me scroll a bit up and yes, here is this variable.
My new variable and its value is test.
And of course now you could easily access this new variable.
Let me clear terminal echo dollar sign my new variable and here is its value.
That is that simple.
Also, when you run some applications like for example, Node.js, Python, Java applications, many
applications require runtime variables and usually you could set such environment variables before start
of any application using similar approach.
All right, that's all about environment variables.
And now you'll get small challenge.
You need to create a new variable called my name my underscore name.
Afterwards, you need to create a new shell script called name Dot as H.
And inside of this new script you need to print your name to the terminal using Echo command.
And afterwards, of course, you need to execute this script.
Wish you good luck guys.
And after the post I'll show you a solution for this challenge.
I'll see you next.
Bye.