Linux 上的退出代码列表
当脚本或进程退出或通过其他方式终止时,它将有一个退出代码,该代码给出有关脚本或进程如何或为何结束的一些指示。例如,退出代码 0
表示进程退出时没有错误 - 换句话说,它完成了任务并按预期退出。另一方面,退出代码 1
表示进程在退出时遇到某种错误。
除了这两个退出代码之外,Bash 中还有许多其他保留代码,它们都有自己的含义。在本教程中,我们将向您展示 Linux 系统上的退出代码列表,并解释每个代码的含义。
在本教程中您将学习:
Linux 中的退出代码是什么?
如何在 Bash 中检索退出代码
Bash 中保留的退出代码列表
Linux 上的退出代码列表
现在您知道什么是退出代码,让我们看看如何检索已结束的脚本或进程的退出代码。例如,我们将运行一个简短的脚本,然后查看其退出代码。查看退出代码的命令是 echo $?
$ ./script.sh
$ echo $?
0
在上面的示例中,退出代码为 0,这意味着脚本按预期终止。
您将在下表中找到退出代码及其含义的列表。
Exit code | Description |
---|---|
0 | Successful exit without errors |
1 | One or more generic errors encountered upon exit |
2 | Incorrect usage, such as invalid options or missing arguments |
126 | Command found but is not executable |
127 | Command not found, usually the result of a missing directory in $PATH variable |
128+N | Command encountered fatal error (was forcefully terminated manually or from an outside source). The N tells us which signal was received (see example below) |
130 | Command terminated with signal 2 (SIGINT) (ctrl+c on keyboard). 128+2 |
143 | Command terminated with signal 15 (SIGTERM) (kill command). 128+15 |
结束语
在本教程中,我们了解了 Bash 中的退出代码是什么、如何检索退出代码以及 Linux 系统上常见退出代码及其含义的列表。对于 Linux 管理员来说,检索退出代码并了解其含义非常重要,以便确定进程或脚本如何或为何结束。退出代码可以告诉您脚本是否正常退出或是否发生错误。