bash 我们可以将 Perl 脚本中使用的变量设置为环境变量吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12406152/
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
Can we set a variable used in a Perl script as environment variable?
提问by Ashish Sharma
I have a Perl script which has a variable like my $name. Can we set the contents of $name as an environment variable which we can import and use in other files?
我有一个 Perl 脚本,它有一个像my $name. 我们可以将 $name 的内容设置为我们可以导入并在其他文件中使用的环境变量吗?
I tried like $ENV{NAME}=name, but this is not working.
我试过了$ENV{NAME}=name,但这不起作用。
回答by Vijay
If you want to affect the environment of your process or your child processes, just use the %ENVhash:
如果要影响进程或子进程的环境,只需使用%ENV哈希:
$ENV{CVSROOT}='<cvs>';
If you want to affect the environment of your parent process, you can't. At least not without cooperation of the parent process. The standard process is to emit a shell script and have the parent process execute that shell script:
如果你想影响你的父进程的环境,你不能。至少不是没有父进程的合作。标准过程是发出一个 shell 脚本并让父进程执行该 shell 脚本:
#!/usr/bin/perl -w
print 'export CVSROOT=<cvs>';
... and call that script from the shell (script) as:
...并从外壳程序(脚本)调用该脚本为:
eval `myscript.pl`
回答by Kevin
Environment variables are specific to a process. When a child process is spawned, it inherits copies of its parent's environment variables, but any changes it makes to them are restricted to itself and any children it spawns after the change.
环境变量特定于进程。生成子进程时,它会继承其父进程的环境变量的副本,但它对它们所做的任何更改都仅限于自身以及更改后生成的任何子进程。
So no, you can't set an environment variable for your shell from within a script you run.
所以不,您不能从您运行的脚本中为您的 shell 设置环境变量。

