我在 bash 脚本中使用“source”,如下所示:
#!/bin/bash
source someneatscriptthatendsprematurely.sh
我想退出 someneatscriptthatendsprematurely.sh 脚本,而不退出主脚本。
感谢任何帮助!
最佳答案
您需要 return
语句:
return [n]
Causes a function to exit with the return value specified by
n
. Ifn
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 eithern
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
关于bash - 你如何返回到源 bash 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3666846/