问题描述
我已经尝试过这些命令.
I have tried those commands.
~$top
(ctrl + z)stopped the process
~$echo $?
147
~$top
(ctrl + c)killed the process
~$echo $?
0
这里发生了什么,请解释一下为什么它显示恒定值.这些值的含义是什么.
What happened here, please explain it and why it showing some constant value. What is the meaning of those values.
推荐答案
$?
是上次运行过程的返回代码. 0表示没有错误发生.其他值表示某种异常情况.
$?
is the return code from the last run process. 0 means no error happened. Other values represent some kind of unusual condition.
值128及以上通常表示某种信号. 147-128 = 19,表示程序接收到信号19(在Linux上为SIGSTOP
).现在,通常按^ Z会发送SIGTSTP
(与SIGSTOP
不同的信号),这可能意味着top
捕获了该信号,进行了一些(可能与终端相关的)清理,然后重新发出SIGSTOP
来实际挂起程序.
Values 128 and above usually represent some kind of signal. 147 - 128 = 19, which means the program received signal 19 (SIGSTOP
on Linux). Now, normally pressing ^Z sends SIGTSTP
(a different signal from SIGSTOP
), which probably meant that top
caught that signal, did some (probably terminal-related) cleanup, and reissued SIGSTOP
to actually suspend the program.
top
也捕获了SIGINT
(通常按^ C后发出),以进行清理并干净地退出(退出值为0).
top
also caught SIGINT
(which is normally issued after pressing ^C), to do cleanup and exit cleanly (with exit value 0).
您可以运行kill -l
来查看当前平台的所有信号编号.请注意,不同平台的数字不同.例如,SIGSTOP
在Darwin上为17,在Linux上为19.
You can run kill -l
to see what all the signal numbers are for the current platform. Note that the numbers are different for different platforms; for example, SIGSTOP
is 17 on Darwin and 19 on Linux.
这篇关于什么是echo $?在Linux终端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!