What are environment variables?

Asked

Viewed 3,271 times

4

I have seen many questions, articles and even some modules in the standard Python library where a term is introduced which is "environment variables". Here at Stackoverflow there are many questions that speak of the same, but always on how to manipulate them, dependent on a certain language. My doubts are:

  • What are environment variables?
  • All languages have the same?
  • When I should wear it?

1 answer

7


What are environment variables?

When a program is run, it receives information from the environment in which it is running. This environmental information is implicitly passed via environment variables. These values are, for example, the locale system, terminal type, etc.

They are dynamically named values in the operating system, which affects the behaviors of the programs that consume them. They are stored in a key-value "list".

All values of environment variables are non-null strings.

Generally, we store settings in these variables, both from the logged-in user on the system, and system-wide. See some environment variables that are configured on my system:

"EDITOR"=>"vim",
"HOME"=>"/Users/vnbrs"
"LANG"=>"en_US.UTF-8"
"PAGER"=>"less"
"PWD"=>"/Users/vnbrs"
"SHELL"=>"/bin/zsh"
"SSH_KEY_PATH"=>"~/.ssh/rsa_id"
"USER"=>"vnbrs"

These settings can be retrieved by system processes. The sub-processes inherit the parent process environment variables.

All languages have the same?

It’s not the languages that have them, it’s the processes. The languages of communication implementations with the operating system Apis to query them. An example with Python:

# test.py
import os
os.environ

And run in the shell:

$ python test.py
# => { 'COLORTERM': 'truecolor', 'PAGER': 'less', ... }

Most UNIX shells allow you to input environment variables by program via command line, see:

$ MINHA_VARIAVEL=meu_valor python test.py
# => { 'MINHA_VARIAVEL': 'meu_valor', 'COLORTERM': 'truecolor', 'PAGER': 'less', ... }

Note that this environment variable will only be available for this process, in this case python.

When I should wear it?

Keys and passwords

There are several uses of environment variables. A frequent use is to store keys and passwords, although there are better methods. Instead of you having several constants in your application to store API keys, database passwords and stuff, why not put them in environment variables?

The development settings:

# development.env
MYSQL_HOST=0.0.0.0
MYSQL_USER=admin
MYSQL_PASSWORD=admin

GITHUB_API_KEY=test
STACKOVERFLOW_API_KEY=test
TRAVIS_CI_API_KEY=test

The production configurations (fictitious):

# production.env
MYSQL_HOST=172.28.281.1
MYSQL_USER=ncuabs9123h
MYSQL_PASSWORD=asduUG&*@!Y*#BSDbis

GITHUB_API_KEY=HASDb9IHU1u2h9
STACKOVERFLOW_API_KEY=oaishdBHSDP(*IH
TRAVIS_CI_API_KEY=ASIJPNDUQH*(@#audsibu

These variables can be dynamically loaded into the application, depending on the environment. Thus, you can hide the keys, not needing to share with those who do not compete this information.

System settings

The operating system and some applications use environment variables to locate themselves. If you develop in Java, you should know the JAVA_HOME. This is how programs know where Java is installed.

For those who "live" in the terminal know that cd ~ will take you to the logged user’s home directory. But where does it get this setting? It’s the environment variable HOME. In my case:

HOME=/Users/vnbrs
  • Thank you for the reply.

  • cd ~ does not lead to root, but rather to the home directory of the logged in user.

  • could complement by saying that environment variables serve for the programmer not to worry about the specific configurations of the machines, for example, database password, endpoint of Apis in production, because, this is the task of network administrators.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.