bash 在 .env 中设置带有空格的变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29422799/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Setting variables with spaces within .env
提问by ThomasReggi
I have a .env
file with a bunch of variables and I just came across an error.
我有一个.env
包含一堆变量的文件,但我遇到了一个错误。
One of the variables has spaces.
其中一个变量有空格。
TEST="hello world"
So when I run this, learned about in this answer here.
所以当我运行这个时,在这个答案中了解到。
env $(<.env)
I get this error.
我收到这个错误。
env: world"': No such file or directory
How do I set variables with spaces within .env?
如何在 .env 中设置带有空格的变量?
回答by Michael Jaros
If your command is just a shell command, you could run your command in a subshell like this:
如果您的命令只是一个 shell 命令,您可以在子 shell 中运行您的命令,如下所示:
( . .env ; echo "$TEST" )
The source
or .
builtin has no problem with assignments containing spaces. It will set the variables in the .env
file in the current shell's environment.
该source
或.
内置有包含空格的任务没有问题。它将.env
在当前 shell 的环境中设置文件中的变量。
In the more likely case of calling an external program, you'll also have to add 'export' to each assignment in your env
file like this:
在更可能调用外部程序的情况下,您还必须为env
文件中的每个分配添加“导出”,如下所示:
export TEST="hello world"
This is necessary because source
does not export assigned variables as env
does, i.e. they are set inside the subshell only but not in the environment of another process started inside that subshell.
这是必要的,因为source
它不像那样导出分配的变量env
,即它们仅在子外壳内设置,但不在该子外壳内启动的另一个进程的环境中。