Environment
Ok, we've come a long way and discussed multiple important topics. Take some time to reflect on what you've learned so far and review your notes - anything you're not too sure about? Let us know!
This is the last page before we move onto this track's challenge.
So, if you were asked to describe the physical environment you are in, you would probably come up with your city, current time, weather, amongst other options.
The command line itself also operates in an environment. For example, and as we've checked already, the command line knows where your home folder is, where to find node and what your username is.
All of this information is stored in environmental variables.
Environmental variables describe the current terminal session. You can see them by typing env
:
Every line on this screenshot is a key/value pair, e.g.:
HOME=/home/jordan
The HOME env variable defines where the home directory is for the current user. You can easily find the variables responsible for setting the username, language, temporary directory, etc.
Every program you run on your computer has access to the environment variables. They help the programs to actually understand what environment they are working in. For example, if you needed to create a temporary file in your JavaScript code, you'd read the value of the TMPDIR
variable to find out where the temporary directory is.
You can print the value of any single environment by typing echo followed by the key:
echo $HOME
In our case, this would print /home/jordan
to the screen.
PATH
The PATH environment variable is one of the most important ones, so it's worth going into a little more detail. It is a colon-separated list of directories that the terminal will look in to find the commands you ask it to run.
On our machine, it looks like: /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
When you type a command without specifying the file path, the terminal will look through each of these directories until it finds the file, and then execute it.
Do you remember before when we looked in /bin
and saw all of those files with the names of the commands we've been learning about? The reason you can, for example, run ls
instead of /bin/ls
is because /bin
is in your PATH.
If you try running /bin/ls
., it should have the same effect as ls
.
If you move your hello.js
file to /usr/local/bin
and rename it from hello.js
to hello
, then typing the command hello
into your terminal should run the program!
This will make you use a couple of the mentioned commands, if you're having issues or just want to discuss the commands then get in touch. We'll be happy to help!
Setting Environment Variables
We can also set environment variables, not just read them.
Give the following a try:
export FAVOURITE_COLOUR=blue
You've just created a new environment variable called FAVOURITE_COLOUR
.
You can read its value back:
echo $FAVOURITE_COLOUR
What if we wanted to modify PATH? Let's add the home directory to the end of PATH:
export PATH=$PATH:$HOME
Why would we want to modify environmental variables, you might be asking? One of the common use cases is storing sensitive data, like passwords.
Let's say you're writing an open source project that uses photos from Facebook. Your code will need to use a secret key that will give you access to Facebook. This key is secret and you shouldn't share it with anyone. However, you want to share your code publicly on a server like Github. How should you do it? Put your secret key in an environment variable and then read it from your code when you need it. For example:
export SECRET_KEY=12345abcde
Then, in your JavaScript code, you would read the value:
const secretKey = process.env.SECRET_KEY
This way you'll be able to share your code with anyone without compromising your keys. Save this as a reference for the future, because you’ll touch on this topic in later modules.