Linux “$”是什么意思?给我们一个shell脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7383144/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 06:09:22  来源:igfitidea点击:

What does "$?" give us exactly in a shell script?

linuxbashshellsh

提问by Khushman Patel

I saw the code written somewhere online, and I wanted to know what exactly does "$?" do/give us. Googling did not help.

我看到网上某处写的代码,我想知道“$”到底是什么?做/给我们。谷歌搜索没有帮助。

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

采纳答案by Brendan Long

$?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

回答by evil otto

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 是成功退出,非零表示某种错误。

回答by Brendan Long

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 whoand the exit status of the command. A script would be the same.

这显示了who的输出和命令的退出状态。脚本将是相同的。

 #!/bin/bash
 who
 echo $?

Output: 0

输出:0

回答by Jens

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:

大多数答案都缺少一些细节。在 shellPOSIX 标准中,在有关特殊参数的部分中找到了明确的答案:

$? 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 lsis grammatically a pipeline consisting of a single command. But then, what is $?for a multi-command pipeline? It's the exit status of the lastcommand in the pipeline.

不要对管道这个词感到惊讶,因为即使是一个简单的命令,例如在ls语法上也是一个由单个命令组成的管道。但是,$?多命令管道是什么?它是管道中最后一个命令的退出状态。

And what about pipelines executing in the background, like grep foo bigfile|head -n 10 > result &?

那么在后台执行的管道grep foo bigfile|head -n 10 > result &呢,比如?

Their exit status can be retrieved through waitonce the pipeline's last command has finished. The background process pid is available as $!, and $?only reports whether the background command was correctly started.

wait一旦管道的最后一个命令完成,就可以检索它们的退出状态。后台进程 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 程序返回其他值可能不会在$?.

回答by craq

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