15

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.

sakthi
  • 675
  • 2
  • 10
  • 18
  • 2
    I'm voting to close this question as off-topic because it should be asked @ http://unix.stackexchange.com/ – Alex K. Feb 17 '16 at 13:25
  • It is the exit status. Read about [bash special parameters](https://www.gnu.org/software/bash/manual/html_node/Special-Parameters.html) – Basile Starynkevitch Feb 17 '16 at 13:29
  • @g-v While technically true, that post doesn't answer what a return code of >=128 means. :-) – C. K. Young Feb 17 '16 at 13:31
  • 1
    @ChrisJester-Young, agree, also http://stackoverflow.com/questions/7294056/how-to-lookup-the-meaning-of-exit-codes-for-linux-command-line-utilities – gavv Feb 17 '16 at 13:35
  • @g-v Jonathan Leffler's answer on that thread is excellent and I upvoted it. Thanks for the pointer! – C. K. Young Feb 17 '16 at 13:36
  • 1
    And finally http://stackoverflow.com/questions/19074956/what-happens-when-you-hit-ctrlz-on-a-process for explanation about `^Z`. – gavv Feb 17 '16 at 13:37

2 Answers2

27

$? is the return code from the last run process. 0 means no error happened. Other values represent some kind of unusual condition.

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 also caught SIGINT (which is normally issued after pressing ^C), to do cleanup and exit cleanly (with exit value 0).


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.

C. K. Young
  • 219,335
  • 46
  • 382
  • 435
5

echo $? returns the return value (exit status) of the last executed command (0 is usually success).

Idos
  • 15,053
  • 14
  • 60
  • 75
  • Hi, well my cpp program was supposed to return 1234 but the echo $? return 210, doesn't echo $? suppose to return also 1234? – Tomer Mar 19 '18 at 21:35