简体   繁体   English

运行 bash 文件后设置或环境变量丢失

[英]Setup or env variable lost after running bash files

Please excuse me if there is an error in the sample code.如果示例代码中有错误,请见谅。

There are two files testA.sh and testB.sh.有两个文件 testA.sh 和 testB.sh。

testA.sh测试A.sh

#!/bin/bash

export a="hello"
source testb.sh

testB.sh测试B.sh

#!/bin/bash

echo "test b"
echo $a

deepak[18:33] $./testA.sh deepak[18:33] $./testA.sh
test b测试b
hello你好

deepak[18:33] $ echo $a deepak[18:33] $ 回声 $a

deepak[18:33] $迪帕克[18:33] $

If we run any setup in the testB.sh then how can we use that setup or env variable in the main console (for example echo $a)如果我们在 testB.sh 中运行任何设置,那么我们如何在主控制台中使用该设置或环境变量(例如 echo $a)

testA.sh runs in a subshell, and it's environment evaporates with it when it ends. testA.sh 在一个子shell 中运行,它的环境在它结束时随之消失。

if you source testA.sh it will keep the value.如果您source testA.sh ,它将保留该值。

$: ./testA.sh
test b
hello
$: echo $a

$: . ./testA.sh
test b
hello
$: echo $a
hello

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM