gpt4 book ai didi

bash - 当没有 “return” 语句时,bash 函数返回什么?

转载 作者:行者123 更新时间:2023-11-29 09:18:32 25 4
gpt4 key购买 nike

bash函数的返回值是最后执行命令的状态吗?

我写了这个测试,看起来是这样的。我只是想验证一下。显然以前没有人问过这个问题,教程也没有提到这一点。

测试程序:

funa() {
echo "in funa";
true;
};

funb() {
echo "in funb"
false;
};

funa && echo "funa is true";
funb && echo "funb is true";

程序运行时的输出:

in funa
funa is true
in funb

有人知道答案吗?

最佳答案

是的。根据 man bash:

Shell Function Definitions

When executed, the exit status of a function is the exit status of the last command executed in the body. (See FUNCTIONS below.)

关于bash - 当没有 “return” 语句时,bash 函数返回什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12151539/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com