Now we'll look at how to manage systemwide environmental profiles.
And Linux first, what is the so called environment?
We can see our current user's environment with a command like this
print E and V or E and V.
What we see here are environment variables.
And let's take this example from the output
his size 1000.
This means that currently the variable called his size is set to 1000
and changing this to another value is as easy as writing
his size equals 2000
at the command-line.
So what does this do? In
this case,
the environment variable is used by bash our
current login shell each time bash runs,
it checks this value to know the maximum size of the command history.
It should save,
we can check out the history of every command we ever
typed in this session and previous sessions with this command.
So with the size of 1000
bash will never save more than 1000 commands in the history.
In this case, an environment variable was used as some sort of program setting
other times applications look at these variables to get an
idea about the sort of environment they're running in.
For example, we'll see something like this variable.
This tells applications where our home directory is.
If they want to save a file,
they can make use of this variable to save it at the right location.
To get an idea of how this works, we can run this command,
adding this dollar sign in front of the variable,
drops the content of that variable in the same spot on the command-line.
So writing Echo home
is the same as writing echo slash home slash
int.
This can be useful in scripts.
Imagine we have a script and we want to save a file in the user's home directory.
We could write this.
Now when the user Aaron runs it,
the variable will fill in slash home slash Aaron in that spot.
So this command will run
if Jane runs it, this command will run.
Now, this is useful because any user can run the script,
but the command will intelligently fill in
a different home directory for each user.
So it dynamically adjusts for every user's environment.
If a user wants to add their own environment variables, they can edit the dot bashrc
file.
We'll see something like this if we explore it.
But in this case,
we want to make sure that we set some variable to
a certain value for every user that logs into the system.
Not just one
to do this, we make changes to this file Etsy environment.
Now, imagine we type this in and save the file.
This variable will now be set every time a user logs in
to test this out in a virtual machine.
After editing the file, you can log out with this command
and then log back in.
Now if we type this,
we'll see this output which confirms the variable has been set accordingly.
But that file is only for setting environment variables.
What if we want to do more complex stuff like run a command every time a user logs in,
we can use the special directory slash Etsy slash profile dot d here,
we can create a file that ends with a dot sh extension.
Let's add a file called lastlogin dot sh.
And we will type in this and then save the file and exit.
This is a simple script that uses the theory we learned about in previous lessons.
It basically logs a date and time when the user logged in
in a file called last login under their home directory.
Now, once again, let's log out and then log back in.
And if we type LS,
we'll see a new file called last login. And then if we type can last login,
we'll see that our script did its job perfectly.
And we even made use of the environment variable dollar sign home
to showcase how we can actually use it in a script.
While these are bash scripts note that the dot sh
files we add to Etsy slash profile dot D.
We don't need a shebang like slash bin slash bash.
The system already knows that anything found in Etsy profile
dot D should be processed under our current shell,
the bash command interpreter.