Bash函数中的返回语句和退出语句在退出代码方面有什么区别?
当前回答
如果将Bash脚本转换为函数,则通常将退出N替换为返回N。调用该函数的代码将把返回值视为子进程的退出代码。
在函数中使用exit将强制整个脚本结束。
其他回答
有时,使用。或来源。
. a.sh
如果在a.s sh中包含了一个exit,它不仅会终止脚本,还会结束shell会话。
如果在a.h sh中包含一个返回值,它就会停止处理脚本。
从man bash on return [n];
使函数停止执行并将n指定的值返回给调用者。如果省略n,返回状态为函数体中最后执行的命令。
... 退出[n]:
使shell以状态n退出。如果省略n,退出状态为最后执行的命令。EXIT上的trap在shell终止之前执行。
编辑:
根据你对问题的编辑,关于退出码,return和退出码没有关系。退出码用于应用程序/脚本,而不是函数。因此,在这方面,唯一设置脚本退出码的关键字(调用程序使用$?Shell变量)是退出。
编辑2:
我提到退出的最后一句话引起了一些评论。这样做是为了区分return和exit,以便理解OP,事实上,在程序/shell脚本的任何给定点上,exit是使用调用进程的退出代码结束脚本的唯一方法。
在shell中执行的每个命令都会产生一个本地“退出码”:它设置$?变量,可以与if, &&和其他操作符一起使用,有条件地执行其他命令。
这些退出码(以及$?变量)将在每次命令执行时重置。
顺便提一下,脚本执行的最后一个命令的退出码被用作调用进程看到的脚本本身的退出码。
最后,函数在被调用时,作为关于退出代码的shell命令。函数的退出码(在函数内)是通过使用return设置的。因此,当在函数中运行return 0时,函数执行将终止,并给出0的退出码。
OP的问题是: BASH函数中的返回语句和退出语句在退出代码方面有什么区别?
首先,需要做一些澄清:
A (return|exit) statement is not required to terminate execution of a (function|shell). A (function|shell) will terminate when it reaches the end of its code list, even with no (return|exit) statement. A (return|exit) statement is not required to pass a value back from a terminated (function|shell). Every process has a built-in variable $? which always has a numeric value. It is a special variable that cannot be set like "?=1", but it is set only in special ways (see below *). The value of $? after the last command to be executed in the (called function | sub shell) is the value that is passed back to the (function caller | parent shell). That is true whether the last command executed is ("return [n]"| "exit [n]") or plain ("return" or something else which happens to be the last command in the called function's code.
在上面的项目符号列表中,从"(x|y)"中选择,要么总是第一项,要么总是第二项,分别获得关于函数和return的语句,或者shell和exit的语句。
很明显,它们都使用了特殊变量$?在值终止后向上传递值。
*现在是$?可设置:
When a called function terminates and returns to its caller then $? in the caller will be equal to the final value of $? in the terminated function. When a parent shell implicitly or explicitly waits on a single sub shell and is released by termination of that sub shell, then $? in the parent shell will be equal to the final value of $? in the terminated sub shell. Some built-in functions can modify $? depending upon their result. But some don't. Built-in functions "return" and "exit", when followed by a numerical argument both set $? with their argument, and terminate execution.
值得注意的是,$?可以通过在子shell中调用exit来赋值,如下所示:
# (exit 259)
# echo $?
3
首先,return是一个关键字,exit是一个函数。
也就是说,这里有一个最简单的解释。
返回
它从函数中返回一个值。
exit
它退出或放弃当前shell。
简单来说(主要针对编程新手),我们可以说:
`return`: exits the function,
`exit()`: exits the program (called as process while running)
如果你观察一下,这是非常基本的,但是…
`return`: is the keyword
`exit()`: is the function
推荐文章
- Linux Bash中双&和分号有什么区别?
- 在Bash中模拟do-while循环
- 如何以及为什么'a'['toUpperCase']()在JavaScript工作?
- 如何从Python函数中返回两个值?
- 在Bash中将输出赋给变量
- 错误:无法找到函数…在R
- 如何在Mac OS X 10.6中使硬件发出哔哔声
- 在编写自己的函数时,如何使用R的省略号特性?
- Rust中的默认函数参数
- 从Docker容器获取环境变量
- 如何重定向标准derr和标准输出到不同的文件在同一行脚本?
- 如何循环通过一个目录递归删除具有某些扩展名的文件
- 在Bash中获取日期(比当前时间早一天)
- Linux: kill后台任务
- 在OSX中永久设置PATH环境变量