shell 脚本中的全局环境变量 [英] Global environment variables in a shell script

查看:52
本文介绍了shell 脚本中的全局环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 bash 脚本中设置全局环境变量?

如果我做类似的事情

#!/bin/bashFOO=酒吧

...或

#!/bin/bash出口 FOO=bar

... vars 似乎留在本地上下文中,而我想在脚本完成执行后继续使用它们.

解决方案

使用 运行你的脚本.

<预><代码>.脚本文件

这将在当前 shell 环境中运行脚本.

export 控制哪些变量可用于新进程,所以如果你说

FOO=1出口 BAR=2./runScript.sh

那么$BARrunScript.sh的环境中是可用的,但是$FOO不会.

How to set a global environment variable in a bash script?

If I do stuff like

#!/bin/bash
FOO=bar

...or

#!/bin/bash
export FOO=bar

...the vars seem to stay in the local context, whereas I'd like to keep using them after the script has finished executing.

解决方案

Run your script with .

. myscript.sh

This will run the script in the current shell environment.

export governs which variables will be available to new processes, so if you say

FOO=1
export BAR=2
./runScript.sh

then $BAR will be available in the environment of runScript.sh, but $FOO will not.

这篇关于shell 脚本中的全局环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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