Bash 从导出变量的源脚本退出
我有一个脚本可以导出一些变量: 我的脚本.shBash 从导出变量的源脚本退出,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
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