I saw the code written somewhere online, and I wanted to know what exactly does "$?" do/give us. Googling did not help.
我看到代码写在网上的某个地方,我想知道“$?”到底是做什么的。google并没有帮助。
Here's the code I saw it in:
这是我看到的代码:
#!/bin/sh
ping -c 2 localhost
if [ $? != 0 ] ; then
echo "Couldn't ping localhost, weird"
fi
ping -c 2 veryweirdhostname.noend
if [ $? != 0 ] ; then
echo "Surprise, Couldn't ping a very weird hostname.."
fi
echo "The pid of this process is $$"
Taken from: http://efod.se/writings/linuxbook/html/shell-scripts.html
来自:http://efod.se/writings/linuxbook/html/shell-scripts.html
5 个解决方案
#1
28
$?
is a variable holding the return value of the last command you ran.
$ ?是一个变量,保存您运行的最后一个命令的返回值。
Example C program (example.c
):
示例C程序(example.c):
int main() { return 1; }
Example Bash:
Bash示例:
gcc -o example example.c
./example
echo $? # prints 1
#2
9
Most of the answers are missing a bit of detail. A definitive answer is found in the POSIX standard for the shell, in the section on special parameters:
大多数答案都缺少一点细节。在关于特殊参数的一节中,在壳体的POSIX标准中找到了明确的答案:
$? Expands to the decimal exit status of the most recent pipeline (see Pipelines ).
$ ?扩展到最近管道的十进制退出状态(见管道)。
Don't be surprised by the word pipeline, because even a simple command such as ls
is grammatically a pipeline consisting of a single command. But then, what is $?
for a multi-command pipeline? It's the exit status of the last command in the pipeline.
不要对“管道”这个词感到惊讶,因为即使是简单的命令,比如ls,语法上也是一条由单个命令组成的管道。那么,什么是美元呢?multi-command管道?它是管道中最后一个命令的退出状态。
And what about pipelines executing in the background, like grep foo bigfile|head -n 10 > result &
?
那么在后台执行的管道呢,比如grep foo bigfile|head - n10 >结果和?
Their exit status can be retrieved through wait
once the pipeline's last command has finished. The background process pid is available as $!
, and $?
only reports whether the background command was correctly started.
一旦管道的最后一个命令完成,它们的退出状态可以通过等待来检索。后台进程pid可用$!,美元?只报告后台命令是否正确启动。
Another detail worth mentioning is that the exit status is usually in the range 0 through 255, with 128 to 255 indicating the process exited due to a signal. Returning other values from a C program is likely to not be reflected accurately in $?
.
另一个值得一提的细节是,退出状态通常在0到255之间,其中128到255表示进程由于信号而退出。从C程序返回其他值可能不会准确地反映在$?中。
#3
6
It's the return code from the most recently executed command.
它是最近执行的命令的返回代码。
By convention 0 is a successful exit and non-zero indicates some kind of error.
按照惯例,0是一个成功的退出,非零表示某种错误。
#4
1
This special variable shows the exit status of the last command that was run in a script or command-line. For example, in a command-line, the user could type
这个特殊的变量显示在脚本或命令行中运行的最后一个命令的退出状态。例如,在命令行中,用户可以输入
who; echo $?
The output would then be
那么输出就是
user tty7 2014-07-13 19:47
0
This shows the output of who and the exit status of the command. A script would be the same.
这显示了who的输出和命令的退出状态。脚本也是一样的。
#!/bin/bash
who
echo $?
Output: 0
输出:0
#5
0
the other answers cover bash pretty well, but you don't specify a shell in your question. In csh (and tcsh) $?
can be used to query the existence of variables, e.g.
其他的答案很好地涵盖了bash,但是在您的问题中没有指定shell。在csh(和tcsh) $?可以用来查询变量的存在。
if $?my_var then
echo my_var exists
endif
#1
28
$?
is a variable holding the return value of the last command you ran.
$ ?是一个变量,保存您运行的最后一个命令的返回值。
Example C program (example.c
):
示例C程序(example.c):
int main() { return 1; }
Example Bash:
Bash示例:
gcc -o example example.c
./example
echo $? # prints 1
#2
9
Most of the answers are missing a bit of detail. A definitive answer is found in the POSIX standard for the shell, in the section on special parameters:
大多数答案都缺少一点细节。在关于特殊参数的一节中,在壳体的POSIX标准中找到了明确的答案:
$? Expands to the decimal exit status of the most recent pipeline (see Pipelines ).
$ ?扩展到最近管道的十进制退出状态(见管道)。
Don't be surprised by the word pipeline, because even a simple command such as ls
is grammatically a pipeline consisting of a single command. But then, what is $?
for a multi-command pipeline? It's the exit status of the last command in the pipeline.
不要对“管道”这个词感到惊讶,因为即使是简单的命令,比如ls,语法上也是一条由单个命令组成的管道。那么,什么是美元呢?multi-command管道?它是管道中最后一个命令的退出状态。
And what about pipelines executing in the background, like grep foo bigfile|head -n 10 > result &
?
那么在后台执行的管道呢,比如grep foo bigfile|head - n10 >结果和?
Their exit status can be retrieved through wait
once the pipeline's last command has finished. The background process pid is available as $!
, and $?
only reports whether the background command was correctly started.
一旦管道的最后一个命令完成,它们的退出状态可以通过等待来检索。后台进程pid可用$!,美元?只报告后台命令是否正确启动。
Another detail worth mentioning is that the exit status is usually in the range 0 through 255, with 128 to 255 indicating the process exited due to a signal. Returning other values from a C program is likely to not be reflected accurately in $?
.
另一个值得一提的细节是,退出状态通常在0到255之间,其中128到255表示进程由于信号而退出。从C程序返回其他值可能不会准确地反映在$?中。
#3
6
It's the return code from the most recently executed command.
它是最近执行的命令的返回代码。
By convention 0 is a successful exit and non-zero indicates some kind of error.
按照惯例,0是一个成功的退出,非零表示某种错误。
#4
1
This special variable shows the exit status of the last command that was run in a script or command-line. For example, in a command-line, the user could type
这个特殊的变量显示在脚本或命令行中运行的最后一个命令的退出状态。例如,在命令行中,用户可以输入
who; echo $?
The output would then be
那么输出就是
user tty7 2014-07-13 19:47
0
This shows the output of who and the exit status of the command. A script would be the same.
这显示了who的输出和命令的退出状态。脚本也是一样的。
#!/bin/bash
who
echo $?
Output: 0
输出:0
#5
0
the other answers cover bash pretty well, but you don't specify a shell in your question. In csh (and tcsh) $?
can be used to query the existence of variables, e.g.
其他的答案很好地涵盖了bash,但是在您的问题中没有指定shell。在csh(和tcsh) $?可以用来查询变量的存在。
if $?my_var then
echo my_var exists
endif