I need to set a system environment variable from a bash script that would be available outside of the current scope. So you would normally export environment variables like this:
我需要从bash脚本中设置一个系统环境变量,该脚本可以在当前范围之外使用。所以你通常会导出这样的环境变量:
export MY_VAR=/opt/my_var
But I need the environment variable to be available at a system level though. Is this possible?
但我需要环境变量在系统级别可用。这可能吗?
4 个解决方案
#1
6
This is the only way I know to do what you want:
这是我知道做你想做的事的唯一方法:
In foo.sh, you have:
在foo.sh中,您有:
#!/bin/bash
echo MYVAR=abc123
And when you want to get the value of the variable, you have to do the following:
当您想获得变量的值时,您必须执行以下操作:
$ eval "$(foo.sh)" # assuming foo.sh is in your $PATH
$ echo $MYVAR #==> abc123
Depending on what you want to do, and how you want to do it, Douglas Leeder's suggestion about using source could be used, but it will source the whole file, functions and all. Using eval, only the stuff that gets echoed will be evaluated.
根据你想做什么以及你想怎么做,可以使用Douglas Leeder关于使用source的建议,但是它将提供整个文件,函数和所有文件。使用eval,只会评估回显的内容。
#2
13
Not really - once you're running in a subprocess you can't affect your parent.
不是真的 - 一旦你在子流程中运行,你就不会影响你的父母。
There two possibilities:
有两种可能性:
1) Source the script rather than run it (see source .):
1)获取脚本而不是运行它(参见source。):
source {script}
2) Have the script output the export commands, and eval that:
2)让脚本输出导出命令,并评估:
eval `bash {script}` OR: eval "$(bash script.sh)"
EDIT: Corrected the second option to be eval rather than source. Opps.
编辑:更正了第二个选项是eval而不是source。哎呀。
#3
1
Set the variable in /etc/profile (create the file if needed). That will essentially make the variable available to every bash process.
在/ etc / profile中设置变量(如果需要,创建文件)。这基本上使变量可用于每个bash过程。
#4
1
Set the variable in /etc/profile (create the file if needed). That will essentially make the variable available to every bash process.
在/ etc / profile中设置变量(如果需要,创建文件)。这基本上使变量可用于每个bash过程。
...to every NEW bash process...
...到每个新的bash过程......
#1
6
This is the only way I know to do what you want:
这是我知道做你想做的事的唯一方法:
In foo.sh, you have:
在foo.sh中,您有:
#!/bin/bash
echo MYVAR=abc123
And when you want to get the value of the variable, you have to do the following:
当您想获得变量的值时,您必须执行以下操作:
$ eval "$(foo.sh)" # assuming foo.sh is in your $PATH
$ echo $MYVAR #==> abc123
Depending on what you want to do, and how you want to do it, Douglas Leeder's suggestion about using source could be used, but it will source the whole file, functions and all. Using eval, only the stuff that gets echoed will be evaluated.
根据你想做什么以及你想怎么做,可以使用Douglas Leeder关于使用source的建议,但是它将提供整个文件,函数和所有文件。使用eval,只会评估回显的内容。
#2
13
Not really - once you're running in a subprocess you can't affect your parent.
不是真的 - 一旦你在子流程中运行,你就不会影响你的父母。
There two possibilities:
有两种可能性:
1) Source the script rather than run it (see source .):
1)获取脚本而不是运行它(参见source。):
source {script}
2) Have the script output the export commands, and eval that:
2)让脚本输出导出命令,并评估:
eval `bash {script}` OR: eval "$(bash script.sh)"
EDIT: Corrected the second option to be eval rather than source. Opps.
编辑:更正了第二个选项是eval而不是source。哎呀。
#3
1
Set the variable in /etc/profile (create the file if needed). That will essentially make the variable available to every bash process.
在/ etc / profile中设置变量(如果需要,创建文件)。这基本上使变量可用于每个bash过程。
#4
1
Set the variable in /etc/profile (create the file if needed). That will essentially make the variable available to every bash process.
在/ etc / profile中设置变量(如果需要,创建文件)。这基本上使变量可用于每个bash过程。
...to every NEW bash process...
...到每个新的bash过程......