What are environmental variables? What is the effect?

  node.js, question

Environmental variable problemThis is my example. What are the environment variables, what are their functions, and what are the differences on mac/linux systems? I have checked some information, but I still don’t quite understand it. In the process of learning node, I was often mentioned this thing, but I don’t know what it is.

Environmental variables are simply given toSystemAndApplication programSet some parameters, specific environment variables include

  1. Name

  2. Parameter value

In this way, when the system starts an application program, these parameters are set by default and can be modified under linux/etc/profile、~/.profileSuch as file to set the system global environment variables, such as:

# other settings
 #   ...
 export my_var 'hello world!'  ;

You can then obtain this value during the running of your program through the following code segment

#include <stdlib.h>
 #include <stdio.h>
 
 int main() {
 const char * my_var = getenv("my_var");
 Printf("my_var = percentage s\n", my_var);
 return 0;
 bracket

Of course, the nodejs program you mentioned can get environment variables like this:

console.log(process.env)

Output something like this:

bracket
 PATH: '/usr/local/bin:/usr/bin',
 JAVAHOME: '/usr/lib/Java',
 ...
 bracket

When executing a subprocess, you can also specifysubprocessYour own environment variables, such as:

var exec = require('child_process').exec;
 
 var child = exec('cat *.js', {
 PATH: '/usr/bin',
 MY_VAR: 'hello world!'
 }, function (error, stdout, stderr) {
 Log ('stdout:' plus stdout);
 Log ('stderr:' plus stderr);
 if (error !  == null) {
 Log ('execrerror:' plus error');
 bracket
 });

Pass an Object to override the default environment variable through the second parameter of the child_process.exec function

This is some of my knowledge and opinions, I hope it will be helpful to you. I can’t speak more specifically, ha ha!