$ 的含义? shell 脚本中的(美元问号)
是什么?
echo $?
shell编程中的意思
What does
echo $?
mean in shell programming?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
是什么?
echo $?
shell编程中的意思
What does
echo $?
mean in shell programming?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(8)
这是最后执行的命令的退出状态。
,命令
true
始终返回状态0
,而false
始终返回状态1
:例如 手册:(可以通过在 shell 中调用
man bash
来访问)按照惯例,退出状态
0
表示成功,非零返回状态表示失败。 在 wikipedia 上了解有关退出状态的更多信息。还有其他类似的特殊变量,您可以在本在线手册中看到: https://www.gnu.org/s/bash/manual/bash.html#Special-Parameters
This is the exit status of the last executed command.
For example the command
true
always returns a status of0
andfalse
always returns a status of1
:From the manual: (acessible by calling
man bash
in your shell)By convention an exit status of
0
means success, and non-zero return status means failure. Learn more about exit statuses on wikipedia.There are other special variables like this, as you can see on this online manual: https://www.gnu.org/s/bash/manual/bash.html#Special-Parameters
$?
返回最后执行的命令的退出值。echo $?
在控制台上打印该值。零表示执行成功,而非零值则映射到各种失败原因。因此在编写脚本时;我倾向于使用以下语法
比较是在等于
0
或不等于0
时进行的。** 基于评论更新:理想情况下,您不应该使用上面的代码块进行比较,请参阅@tripleee评论和解释。
$?
returns the exit value of the last executed command.echo $?
prints that value on console. zero implies a successful execution while non-zero values are mapped to various reason for failure.Hence when scripting; I tend to use the following syntax
The comparison is to be done on equals to
0
or not equals0
.** Update Based on the comment: Ideally, you should not use the above code block for comparison, refer to @tripleee comments and explanation.
echo $? - 给出最近执行的命令的退出状态。此退出状态很可能是一个数字,其中零表示成功,任何非零值表示失败
? - 这是 bash 中的一个特殊参数/变量。
$? - 它给出存储在变量“?”中的值。
BASH 中一些类似的特殊参数有 1,2,*,#(通常在 echo 命令中看到为 $1 ,$2 , $* , $# 等)。
echo $? - Gives the EXIT STATUS of the most recently executed command . This EXIT STATUS would most probably be a number with ZERO implying Success and any NON-ZERO value indicating Failure
? - This is one special parameter/variable in bash.
$? - It gives the value stored in the variable "?".
Some similar special parameters in BASH are 1,2,*,# ( Normally seen in echo command as $1 ,$2 , $* , $# , etc., ) .
它具有命令的最后一个状态代码(退出值)。
It has the last status code (exit value) of a command.
最小 POSIX C 退出状态示例
要理解
$?
,您必须首先了解定义的进程退出状态的概念 POSIX。在 Linux 中:当进程调用
exit
系统调用时,即使在进程终止后,内核也会存储传递给系统调用的值(int
)。< /p>退出系统调用由
exit()
ANSI C 函数调用,并在您从main
return
时间接调用。调用退出子进程的进程 (Bash),通常使用
fork
+exec
,可以使用wait< 检索子进程的退出状态/code> 系统调用
考虑 Bash 代码:
C “等效”是:
false.c
bash.c
编译并运行:
输出:
在 Bash 中,当您按 Enter 时,会像上面一样发生 fork + exec + wait,然后 bash 设置
$?
到分叉进程的退出状态。注意:对于像
echo
这样的内置命令,不需要生成进程,Bash 只需将$?
设置为 0 即可模拟外部进程。标准和文档
POSIX 7 2.5.2“特殊参数”http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_05_02:
man bash
“特殊参数”:ANSI C 和 POSIX 建议:
0
表示程序成功其他值:程序因某种原因失败。
确切的值可以指示失败的类型。
ANSI C 没有定义任何值的含义,POSIX 指定大于 125 的值:“POSIX”是什么意思?
Bash 使用退出状态
if
在 Bash 中,我们经常使用退出状态
$?
隐式地控制if
语句,如下所示:where
true
是一个只返回 0 的程序。上面相当于:
And in:
[
只是一个具有奇怪名称的程序(Bash 内置程序的行为与此类似),并且1 = 1 ]
其参数,另请参阅: 单和双括号之间的区别Bash 中的双方括号Minimal POSIX C exit status example
To understand
$?
, you must first understand the concept of process exit status which is defined by POSIX. In Linux:when a process calls the
exit
system call, the kernel stores the value passed to the system call (anint
) even after the process dies.The exit system call is called by the
exit()
ANSI C function, and indirectly when you doreturn
frommain
.the process that called the exiting child process (Bash), often with
fork
+exec
, can retrieve the exit status of the child with thewait
system callConsider the Bash code:
The C "equivalent" is:
false.c
bash.c
Compile and run:
Output:
In Bash, when you hit enter, a fork + exec + wait happens like above, and bash then sets
$?
to the exit status of the forked process.Note: for built-in commands like
echo
, a process need not be spawned, and Bash just sets$?
to 0 to simulate an external process.Standards and documentation
POSIX 7 2.5.2 "Special Parameters" http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_05_02 :
man bash
"Special Parameters":ANSI C and POSIX then recommend that:
0
means the program was successfulother values: the program failed somehow.
The exact value could indicate the type of failure.
ANSI C does not define the meaning of any vaues, and POSIX specifies values larger than 125: What is the meaning of "POSIX"?
Bash uses exit status for
if
In Bash, we often use the exit status
$?
implicitly to controlif
statements as in:where
true
is a program that just returns 0.The above is equivalent to:
And in:
[
is just an program with a weird name (and Bash built-in that behaves like it), and1 = 1 ]
its arguments, see also: Difference between single and double square brackets in Bash来自 http://www.gnu.org/s/bash/手册/bash.html#特殊参数
From http://www.gnu.org/s/bash/manual/bash.html#Special-Parameters
请参阅 Bash 手册 www.gnu.org/s/bash/manual/bash.html#Special-Parameters" rel="nofollow noreferrer">3.4.2 特殊参数:
它有点难以找到,因为它没有列为
$?
(变量名称“只是”?
)。当然,另请参阅 退出状态 部分; -)快乐编码。
See The Bash Manual under 3.4.2 Special Parameters:
It is a little hard to find because it is not listed as
$?
(the variable name is "just"?
). Also see the exit status section, of course ;-)Happy coding.
输出最后执行的unix命令的结果
Outputs the result of the last executed unix command