将环境变量传递给进程

时间:2021-12-20 23:03:36

I've just come across this code snippet:

我刚刚看到这个代码片段:

$ DEVELOPMENT=1 node dev-mode.js

This line runs node.js program and sets the environment variable DEVELOPMENT. I don't understand what the mechanism is used to set env variable? Is this the valid syntax:

这条线运行节点。js程序,设置环境变量开发。我不明白设置env变量的机制是什么?这是有效的语法吗?

$ [var1=value1 var2=value2] [process_name process_params] ?

3 个解决方案

#1


2  

There are two ways to set the pass variables from your current shell a running program,

有两种方法可以设置当前shell中的传递变量a正在运行的程序,

Either use the export built-in with syntax as

或者使用内置语法的导出

$ export MYVALUE=5
$ echo "MYVALUE is $MYVALUE"
MYVALUE is 5

This syntax allows the variable to take effect in current shell in all the subsequent sub-shells you are invoking( for command-substitution or process-substitution, etc) and the variable stays alive even after the sub-shells are terminated.

这种语法允许变量在当前shell中对您正在调用的所有后续子shell(用于命令替换或进程替换等)起作用,并且即使在子shell终止之后,该变量仍然保持活动状态。

(or) as asked in the question, if you directly send it to the command as

(或)在问题中,如果你直接将它发送到命令。

$ MYVALUE=5 bash -c 'echo "MYVALUE is $MYVALUE"'
MYVALUE is 5

the value is passed only to the sub-shell(the one started with bash -c) and has no effect on parent shell once it exits. You can observe the MYVALUE from the above syntax now, it will be empty.

该值仅传递给子shell(以bash -c开头的shell),并且在父shell退出时对它没有影响。现在可以从上面的语法中观察MYVALUE,它将是空的。

$ echo $MYVALUE
$

Hope this answers your question.

希望这能回答你的问题。

#2


1  

This example should demonstrate:

这个例子应该展示:

X=0; Y=1;       #set X and Y
#set X, Y for the child and have it expand and print those vars
X=42 Y=43  sh -c 'echo $X $Y' 
#print the same vars in the parent
echo $X $Y      

The

var0=val0 var1=val1... command

syntax makes it act as if the variable list preceding the command was temporarily assigned the corresponding values and the variables were exported as with

语法使它的行为就好像命令前面的变量列表被临时分配了相应的值,变量被导出了

export var0

After the command runs, the variables in the variable list have their original values and export status (=whether they automatically propagate to child processes) restored.

运行命令后,变量列表中的变量将恢复它们的原始值和导出状态(=它们是否自动传播到子进程)。

The example prints

打印的例子

42 43 
0 1

#3


1  

Yes, it's a valid syntax, it's described in Simple Commands section of the man bash:

是的,这是一个有效的语法,在man bash的简单命令部分中有描述:

A simple command is a sequence of optional variable assignments followed by blank-separated words and redirections, and terminated by a control operator. The first word specifies the command to be executed, and is passed as argument zero. The remaining words are passed as arguments to the invoked command.

一个简单的命令是一系列可选的变量赋值,后面跟着空格分隔的单词和重定向,最后由控制操作符终止。第一个单词指定要执行的命令,并作为参数0传递。其余的单词作为参数传递给被调用的命令。

#1


2  

There are two ways to set the pass variables from your current shell a running program,

有两种方法可以设置当前shell中的传递变量a正在运行的程序,

Either use the export built-in with syntax as

或者使用内置语法的导出

$ export MYVALUE=5
$ echo "MYVALUE is $MYVALUE"
MYVALUE is 5

This syntax allows the variable to take effect in current shell in all the subsequent sub-shells you are invoking( for command-substitution or process-substitution, etc) and the variable stays alive even after the sub-shells are terminated.

这种语法允许变量在当前shell中对您正在调用的所有后续子shell(用于命令替换或进程替换等)起作用,并且即使在子shell终止之后,该变量仍然保持活动状态。

(or) as asked in the question, if you directly send it to the command as

(或)在问题中,如果你直接将它发送到命令。

$ MYVALUE=5 bash -c 'echo "MYVALUE is $MYVALUE"'
MYVALUE is 5

the value is passed only to the sub-shell(the one started with bash -c) and has no effect on parent shell once it exits. You can observe the MYVALUE from the above syntax now, it will be empty.

该值仅传递给子shell(以bash -c开头的shell),并且在父shell退出时对它没有影响。现在可以从上面的语法中观察MYVALUE,它将是空的。

$ echo $MYVALUE
$

Hope this answers your question.

希望这能回答你的问题。

#2


1  

This example should demonstrate:

这个例子应该展示:

X=0; Y=1;       #set X and Y
#set X, Y for the child and have it expand and print those vars
X=42 Y=43  sh -c 'echo $X $Y' 
#print the same vars in the parent
echo $X $Y      

The

var0=val0 var1=val1... command

syntax makes it act as if the variable list preceding the command was temporarily assigned the corresponding values and the variables were exported as with

语法使它的行为就好像命令前面的变量列表被临时分配了相应的值,变量被导出了

export var0

After the command runs, the variables in the variable list have their original values and export status (=whether they automatically propagate to child processes) restored.

运行命令后,变量列表中的变量将恢复它们的原始值和导出状态(=它们是否自动传播到子进程)。

The example prints

打印的例子

42 43 
0 1

#3


1  

Yes, it's a valid syntax, it's described in Simple Commands section of the man bash:

是的,这是一个有效的语法,在man bash的简单命令部分中有描述:

A simple command is a sequence of optional variable assignments followed by blank-separated words and redirections, and terminated by a control operator. The first word specifies the command to be executed, and is passed as argument zero. The remaining words are passed as arguments to the invoked command.

一个简单的命令是一系列可选的变量赋值,后面跟着空格分隔的单词和重定向,最后由控制操作符终止。第一个单词指定要执行的命令,并作为参数0传递。其余的单词作为参数传递给被调用的命令。