如何我出口在bash系统范围的环境变量 [英] How to I export a system scope environment variable in bash

查看:171
本文介绍了如何我出口在bash系统范围的环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从一个bash脚本,将是当前范围外提供设置系统环境变量。所以,你通常会出口环境变量是这样的:

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:

出口MY_VAR = /选择/ my_var

export MY_VAR=/opt/my_var

但我需要的环境变量,可在系统级虽然。这可能吗?

But I need the environment variable to be available at a system level though. Is this possible?

推荐答案

这是我知道你想要做什么的唯一方式:

This is the only way I know to do what you want:

在foo.sh,您有:

In foo.sh, you have:

#!/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

根据你想要做什么,以及要如何做到这一点,可以使用道格拉斯Leeder介绍的关于使用源的建议,但它会输出整个文件,功能和所有。使用得到回应将被评估的eval,只有东西。

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.

这篇关于如何我出口在bash系统范围的环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆