如何在bash中导出变量 [英] How to export a variable in bash

查看:94
本文介绍了如何在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:

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

根据您想做什么以及您想怎么做,可以使用道格拉斯·里德(Douglas 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天全站免登陆