从导出变量的源脚本退出

时间:2019-05-15 07:21:16

标签: bash shell

我有一个导出一些变量的脚本:

my_script.sh

export var="Hello"

如果在脚本完成持久化变量后从终端执行此脚本,则可以执行以下操作:

source my_script.sh

. my_script.sh

现在,我想在脚本的某处添加逻辑以从中间退出。如果我只是在我的 my_script.sh 中使用exit 0,则会以上述运行方式关闭当前终端。我想从嵌套函数内部的脚本中退出,所以return关键字不会起作用。

修改: 流程示例:

test() {
 export a='Make this variable to be exported'
 test2
 echo 'make this command gone'
}

test2() {
 return
}

test

有什么办法做到这两种:从脚本中间退出并从脚本中间导出变量?

1 个答案:

答案 0 :(得分:1)

您每次嵌套时可能都需要添加return语句检查。即。

test() {
 export a='Make this variable to be exported'
 test2 || return 1
 echo 'make this command gone'
}

test2() {
 return 1
}

test
相关问题