linux shell中如何接收程序返回值

2025-03-13 13:29:48
推荐回答(1个)
回答1:

$?
$?就是表示上一次脚本或者命令退出时的返回值。通常,0代表成功;非0代表出现错误。
类似的一些符号如$0 $1 $2 $@ $# 。

Before we move on, there is a perversity about tests in Bash shells that I want to discuss. It turns out, because of a historical accident that now might as well be cast in concrete, when a test is conducted or a command returns a result value, the numerical value for "true" is 0, and "false" is 1. Those of you who have some programming experience will likely find this reversal of intuition as annoying as I do.

Here is a way to get the result of the most recent logical test (and to show the weird reversal described above):

$ test -e .
$ echo $?
0

$ test -e xyz
$ echo $?
1