Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Bash 从导出变量的源脚本退出_Bash_Shell - Fatal编程技术网

Bash 从导出变量的源脚本退出

Bash 从导出变量的源脚本退出,bash,shell,Bash,Shell,我有一个脚本可以导出一些变量: 我的脚本.sh export var="Hello" 如果我在脚本结束时从终端执行此脚本以持久化变量,我可以使用: source my_script.sh 或 现在我想在脚本的某个地方添加逻辑,以便从中间退出。如果我只是在我的my_script.sh中使用exit 0,这将以上述运行方式关闭当前终端。我想退出嵌套函数中的脚本,因此return关键字不起作用 编辑: 流程示例: test() { export a='Make this variable to

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

我的脚本.sh

export var="Hello"
如果我在脚本结束时从终端执行此脚本以持久化变量,我可以使用:

source 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

有什么方法可以同时执行这两项操作:从脚本中间退出并从中导出变量?

您可能需要在每次嵌套时添加返回语句检查。即

test(){
export a='Make this variable to export'
测试2 | |返回1
echo“使此命令消失”
}
test2(){
返回1
}
测试

请查看我对@lnian的评论
test() {
 export a='Make this variable to be exported'
 test2
 echo 'make this command gone'
}

test2() {
 return
}

test