如何返回到源代码的bash脚本?

我使用一个脚本扩展使用bash源功能;
#!/bin/bash

source someneatscriptthatendsprematurely.sh

我想能够从那个脚本返回,而不破坏主脚本。

使用退出断点的主脚本,返回只在函数中有效,并尝试$(退出1)似乎也不工作。

那么,是否可以返回一个sub-bash脚本而不打破主bash?

任何帮助赞赏!

你需要return语句:

return [n]
Causes a function to exit with the return value specified by n. If n is omitted,the return status is that of the last command executed in the function body. If used outside a function,but during execution of a script by the . (source) command,it causes the shell to stop executing that script and return either n or the exit status of the last command executed within the script as the exit status of the script. If used outside a function and not during execution of a script by .,the return status is false. Any command associated with the RETURN trap is executed before execution resumes after the function or script.

您可以使用以下两个脚本查看此操作:

script1.sh:
    . script2.sh
    echo hello again
script2.sh:
    echo hello
    return
    echo goodbye

当您运行script1.sh时,您会看到:

hello
hello again

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...