简体   繁体   English

shell 脚本中的全局环境变量

[英]Global environment variables in a shell script

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. ...变量似乎保留在本地上下文中,而我想在脚本执行完毕后继续使用它们。

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 export控制哪些变量可用于新进程,所以如果你说

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不会。

When you run a shell script, it's done in a sub-shell so it cannot affect the parent shell's environment.当您运行shell 脚本时,它是在子 shell 中完成的,因此它不会影响父 shell 的环境。 You want to source the script by doing:您想通过执行以下操作来获取脚本:

. ./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]源文件名 [参数]

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.如果文件名不包含斜杠,则使用 PATH 中的文件名来查找包含文件名的目录。

The file searched for in PATH need not be executable.在 PATH 中搜索的文件不必是可执行文件。 When bash is not in POSIX mode, the current directory is searched if no file is found in 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.如果提供了任何参数,它们将在执行 filename 时成为位置参数。

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。

source myscript.sh is also feasible. source myscript.sh也是可行的。

Description for linux command source : linux命令source说明:

source is a Unix command that evaluates the file following the command, 
as a list of commands, executed in the current context
#!/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. shell 应将导出属性赋予与指定名称对应的变量,这将使它们处于随后执行的命令的环境中。 If the name of a variable is followed by = word, then the value of that variable shall be set to word.如果变量名后跟= word,则该变量的值应设置为word。

A common design is to have your script output a result, and require the cooperation of the caller.一个常见的设计是让你的脚本 output 有一个结果,并且需要调用者的配合。 Then you can say, for example,然后你可以说,例如,

eval "$(yourscript)"

or perhaps less dangerously或者可能不那么危险

cd "$(yourscript)"

This extends to tools in other languages besides shell script.这扩展到除 shell 脚本之外的其他语言的工具。

In your shell script, write the variables to another file like below and source these files in your ~/.bashrc or ~/.zshrc在您的 shell 脚本中,将变量写入另一个文件,如下所示,并在您的~/.bashrc~/.zshrc .zshrc 中获取这些文件

echo "export FOO=bar" >> environment.sh

In your ~/.bashrc or ~/.zshrc , source it like below:在你的~/.bashrc~/.zshrc中,像下面这样获取它:

source Path-to-file/environment.sh

You can then access it globally.然后您可以全局访问它。

FOO=bar
export FOO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM