An "environment variable" is just a simple piece of data that is made available to all running programs. Environment variables exist in both Windows and Unix / Linux, but they are somewhat more prominent in Unix / Linux. One common use-case for environment variables is to parameterize a running program. For example, perhaps you have a web application that needs to know where to look for its database. In testing the database is probably running on the same server as the application so the database would exist at "localhost:2020" or something like that. But in production the database is probably running on another machine, maybe "database:3030". We can specify this with an environment variable set when the program is started:
$ DATABASE_HOST=localhost:2020 ./web_app
This runs the file called "web_app" (the "./" is required to run an executable located in the current directory) and injects the value "localhost:2020" into its environment under the name "DATABASE_HOST". For example, we can inject an environment variable into the Python interpreter (REPL) like this:
$ AWESOME_VARIABLE="i'm awesome" python3
Python 3.6.5 (default, Apr 1 2018, 05:46:30)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> print(os.environ.get('AWESOME_VARIABLE'))
i'm awesome
I injected an environment variable into Python and then accessed it from within a script. This is how environment variables are used, and they are used for many, many purposes. You can explore the other environment variables defined on your machine by printing out the "os.environ" data structure. You can also see a nice list of all environment variables present in your shell using the "env" command.
Note that when you work with environment variables in the shell you need to prepend a "$" character to the variable name in order to get its value. This is how the shell knows you want the value. Every programming language has a slightly different way of accessing environment variables, this is just how the shell does it.
By default, environment variables only exist within the current process (your shell). So, for example, you can set a variable on one line of your shell, but it won't be available if you then run Python later:
$ MY_VARIABLE='hi there'
$ echo $MY_VARIABLE
hi there
$ ipython3
Python 3.6.5 (default, Apr 1 2018, 05:46:30)
Type "copyright", "credits" or "license" for more information.
IPython 5.5.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: import os
In [2]: os.environ.get('MY_VARIABLE')
We can tell the shell that we want the variable to be available inside of all programs we run by exporting it. To do this, just issue the command "export VARIABLE_NAME". You can also export and assign in one step: "export VARIABLE_NAME=value".
$ export MY_VARIABLE='hi there'
$ echo $MY_VARIABLE
hi there
$ ipython3
Python 3.6.5 (default, Apr 1 2018, 05:46:30)
Type "copyright", "credits" or "license" for more information.
IPython 5.5.0 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]: import os
In [2]: os.environ.get('MY_VARIABLE')
Out[2]: 'hi there'
The PATH environment variable is the variable the shell uses to find executable programs. Try "echo $PATH", you'll see something that looks vaguely like this:
$ echo $PATH
/home/george/local/nvm/versions/node/v8.11.3/bin:/home/george/local/go/bin:/home/george/.local/bin:/home/george/bin:/home/george/local/bin:/home/george/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/home/george/Go/bin:/home/george/local/goland/bin:/home/george/local/google-cloud-sdk/bin:/usr/lib/dart/bin:/home/george/.pub-cache/bin:/home/george/local/flutter/bin:/home/george/local/julia:/home/george/local/powershell:/home/george/Android/Sdk/emulator
Mine has a lot of stuff in it because I've customized it to find programs that are either installed in non-standard locations or programs I've manually installed for one reason or another.
The format here is just a list of directories separated by colons ":". There's nothing tricky going on. Why would you want to add something to your PATH? To make it easier to run. Instead of providing the entire path to an executable file, you can just type the name of the file itself. Since PATH is exported it is also made available to other programs, and this allows them, mostly shell scripts, to run programs you have installed as well.