将变量从一个 shell 脚本导出到 bash 中的另一个脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30241044/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 12:59:49  来源:igfitidea点击:

Exporting variable from one shell script to another script in bash

bashshell

提问by vikeng21

I am a newbie in Shell Scripting and i am trying to export a variable from child script to a parent script.below is the code and unfortunately its not working. not sure what is the issue. below is the code.

我是 Shell Scripting 的新手,我正在尝试将变量从子脚本导出到父脚本。下面是代码,不幸的是它不起作用。不知道是什么问题。下面是代码。

I have a script set-vars1.sh code is as below

我有一个脚本 set-vars1.sh 代码如下

#!/bin/bash
line_write="R"
FLAG=''
if [[ $line_write > " " && $line_write == "R" ]]
 then
  echo "Exporting"
  export FLAG=Y
  #export $FLAG;
elif [[  $line_write > " " && $line_write== "N" ]]
  then
  export FLAG=N
fi

EXIT_STATUS=-1
exit $EXIT_STATUS

I am trying to call this from set-vars2.sh the code is as below

我试图从 set-vars2.sh 调用它,代码如下

#!/bin/bash
./set-vars1.sh
 echo $FLAG

When i try to run set-vars2.sh i am not able to echo the value of FLAG and its always blank.

当我尝试运行 set-vars2.sh 时,我无法回应 FLAG 的值并且它始终为空白。

Can you please let me know what is wrong here and how i can correct the same. been breaking my head a long time on this. so any help would be hugely helpful

你能告诉我这里有什么问题吗,我该如何纠正。很长一段时间我都在为此伤脑筋。所以任何帮助都会非常有帮助

回答by Jahid

use source:

使用source

source ./set-vars1.sh

Or:

或者:

. ./set-vars1.sh
#The first . is intentional