shell脚本中的全局环境变量

时间:2021-12-01 11:08:36

How to set a global environment variable in a bash script?

如何在bash脚本中设置全局环境变量?

If I do stuff like

如果我喜欢的话

#!/bin/bash
FOO=bar

...or

...要么

#!/bin/bash
export FOO=bar

...the vars seem to stay in the local context, whereas I'd like to keep using them after the script has finished executing.

... vars似乎停留在本地环境中,而我希望在脚本执行完毕后继续使用它们。

5 个解决方案

#1


125  

Run your script with .

运行您的脚本。

. myscript.sh

This will run the script in the current shell environment.

这将在当前shell环境中运行脚本。

export governs which variables will be available to new processes, so if you say

导出管理哪些变量可用于新进程,所以如果你说

FOO=1
export BAR=2
./runScript.sh

then $BAR will be available in the environment of runScript.sh, but $FOO will not.

然后$ BAR将在runScript.sh的环境中可用,但$ FOO不会。

#2


47  

When you run a shell script, it's done in a sub-shell so it cannot affect the parent shell's environment. You want to source the script by doing:

当您运行shell脚本时,它在子shell中完成,因此它不会影响父shell的环境。您希望通过执行以下操作来获取脚本:

. ./setfoo.sh

This executes it in the context of the current shell, not as a sub shell.

这在当前shell的上下文中执行,而不是作为子shell。

From the bash man page:

从bash手册页:

. filename [arguments]
source filename [arguments]

。 filename [参数]源文件名[参数]

Read and execute commands from filename in the current shell environment and return the exit status of the last command executed from filename.

在当前shell环境中从filename读取并执行命令,并返回从filename执行的最后一个命令的退出状态。

If filename does not contain a slash, file names in PATH are used to find the directory containing filename.

如果filename不包含斜杠,则使用PATH中的文件名来查找包含filename的目录。

The file searched for in PATH need not be executable. When bash is not in POSIX mode, the current directory is searched if no file is found in PATH.

在PATH中搜索的文件不需要是可执行的。当bash未处于POSIX模式时,如果在PATH中找不到文件,则搜索当前目录。

If the sourcepath option to the shopt builtin command is turned off, the PATH is not searched.

如果关闭shopt内置命令的sourcepath选项,则不搜索PATH。

If any arguments are supplied, they become the positional parameters when filename is executed.

如果提供了任何参数,则在执行文件名时它们将成为位置参数。

Otherwise the positional parameters are unchanged. The return status is the status of the last command exited within the script (0 if no commands are executed), and false if filename is not found or cannot be read.

否则,位置参数不变。返回状态是脚本中退出的最后一个命令的状态(如果没有执行命令,则为0),如果未找到或无法读取文件名,则返回false。

#3


6  

source myscript.sh is also feasible.

source myscript.sh也是可行的。

Description for linux command source:

linux命令源的描述:

source is a Unix command that evaluates the file following the command, 
as a list of commands, executed in the current context

#4


4  

#!/bin/bash
export FOO=bar

or

要么

#!/bin/bash
FOO=bar
export FOO

man export:

男人出口:

The shell shall give the export attribute to the variables corresponding to the specified names, which shall cause them to be in the environment of subsequently executed commands. If the name of a variable is followed by = word, then the value of that variable shall be set to word.

shell应将export属性赋予与指定名称对应的变量,这将使它们处于随后执行的命令的环境中。如果变量的名称后跟= word,则该变量的值应设置为word。

#5


-3  

FOO=bar
export FOO

#1


125  

Run your script with .

运行您的脚本。

. myscript.sh

This will run the script in the current shell environment.

这将在当前shell环境中运行脚本。

export governs which variables will be available to new processes, so if you say

导出管理哪些变量可用于新进程,所以如果你说

FOO=1
export BAR=2
./runScript.sh

then $BAR will be available in the environment of runScript.sh, but $FOO will not.

然后$ BAR将在runScript.sh的环境中可用,但$ FOO不会。

#2


47  

When you run a shell script, it's done in a sub-shell so it cannot affect the parent shell's environment. You want to source the script by doing:

当您运行shell脚本时,它在子shell中完成,因此它不会影响父shell的环境。您希望通过执行以下操作来获取脚本:

. ./setfoo.sh

This executes it in the context of the current shell, not as a sub shell.

这在当前shell的上下文中执行,而不是作为子shell。

From the bash man page:

从bash手册页:

. filename [arguments]
source filename [arguments]

。 filename [参数]源文件名[参数]

Read and execute commands from filename in the current shell environment and return the exit status of the last command executed from filename.

在当前shell环境中从filename读取并执行命令,并返回从filename执行的最后一个命令的退出状态。

If filename does not contain a slash, file names in PATH are used to find the directory containing filename.

如果filename不包含斜杠,则使用PATH中的文件名来查找包含filename的目录。

The file searched for in PATH need not be executable. When bash is not in POSIX mode, the current directory is searched if no file is found in PATH.

在PATH中搜索的文件不需要是可执行的。当bash未处于POSIX模式时,如果在PATH中找不到文件,则搜索当前目录。

If the sourcepath option to the shopt builtin command is turned off, the PATH is not searched.

如果关闭shopt内置命令的sourcepath选项,则不搜索PATH。

If any arguments are supplied, they become the positional parameters when filename is executed.

如果提供了任何参数,则在执行文件名时它们将成为位置参数。

Otherwise the positional parameters are unchanged. The return status is the status of the last command exited within the script (0 if no commands are executed), and false if filename is not found or cannot be read.

否则,位置参数不变。返回状态是脚本中退出的最后一个命令的状态(如果没有执行命令,则为0),如果未找到或无法读取文件名,则返回false。

#3


6  

source myscript.sh is also feasible.

source myscript.sh也是可行的。

Description for linux command source:

linux命令源的描述:

source is a Unix command that evaluates the file following the command, 
as a list of commands, executed in the current context

#4


4  

#!/bin/bash
export FOO=bar

or

要么

#!/bin/bash
FOO=bar
export FOO

man export:

男人出口:

The shell shall give the export attribute to the variables corresponding to the specified names, which shall cause them to be in the environment of subsequently executed commands. If the name of a variable is followed by = word, then the value of that variable shall be set to word.

shell应将export属性赋予与指定名称对应的变量,这将使它们处于随后执行的命令的环境中。如果变量的名称后跟= word,则该变量的值应设置为word。

#5


-3  

FOO=bar
export FOO