Eli5: Can Somebody explain to me what are environment variables? In windows, mac, when use them and examples like if i was 5

278 views

Eli5: Can Somebody explain to me what are environment variables? In windows, mac, when use them and examples like if i was 5

In: 3

4 Answers

Anonymous 0 Comments

They’re variables that you configure outside your application/code. The OS provides them to your executable at runtime. A very simple example is the %path% variable. It allows your app to run an executable without having to specify the full path for it, as long as it is listed in the list of paths in that variable.

Their main use is to allow applications to run on different systems without many modifications to internal variables and having to reconfigure a billion things.

Anonymous 0 Comments

Environment variables are just ways to name pieces of text to make them easier to use across multiple programs and scripts. They are used mostly for settings and locations of files.

For example Bash and most other shells use $PATH to search for executables in a specific order when trying to run a command. Updating $PATH allows you to add custom places to check for other programs.

‘export PATH=/opt/bin:$PATH’ will update $PATH and Bash will look for executables in /opt/bin before anywhere else.

Environment variables are used quite extensively when compiling code to make it easier to pass flags to the compiler without having to type them in every time (modify defaults). They are also used in many shell scripts (uncompiled command list) so that you can change 1 line at the top of the scrips (such as a location of a log file) once instead of all over the script and reuse it.

Environment variables can contain mostly any text, and be given an arbitrary name to make sure things easier to read and update. There are tons of well known ones, and lots are used across multiple programs. These are generally documented for your specific shell and in the help for your specific program.

Anonymous 0 Comments

They’re global data that a program might want to know. Often paths common programs.

“windir” for example, is the path to the Windows install, on my PC that is “C:WINDOWS”

The Environment variable “NUMBER_OF_PROCESSORS” on my PC is “16”

So, instead of needing to figure out the directory a programmer can just use “windir” and get the Windows path.

Anonymous 0 Comments

Environment variables are variables that can be set differently in different environments.

A variable is a container that you can put some data in. An environment is a word that’s a bit hard to define, but it basically means a system or a set of systems that is used for a specific purpose.

To explain this with examples might be easier.

At work we do software development. The developers use the development environment. This is a collection of servers that we have dedicated for developers to use for development. Once the developers have developed their code they will copy it to the test environment, which is a collection of servers that is set up for testers to use. Once it passes testing we move it to the Production environment and the code is live.

Now imagine that this code needs to open a file. We have three copies of this file. One in the development environment, one in the test environment, and one in production. The files are all in different locations, so in each environment we store the location of the file for that environment in an environment variable. Then when the code needs the file it can get the correct file path from the environment variable, which is called like “file name” in all environments, so the code only needs to know the name of the environment variable to get the correct file path for the environment that it’s running in.