Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I came across a shell script where echo $? was used. What does this mean . How can we use this in a shell script. Are there any references for this.

share|improve this question
up vote 4 down vote accepted

echo $? means status of your previous command

see the example here

[root@localhost ~]# jk
-bash: jk: command not found
[root@localhost ~]# echo $?
[root@localhost ~]# pwd
[root@localhost ~]# echo $?
[root@localhost ~]# 

At first you are getting 1 because command is wrong And second time you are getting 0 because command was successful

There is no command called jk (in my case)

share|improve this answer

$? returns the result of the last run command in a bash (and probably other) shell.

From example 6-1:


echo hello
echo $?    # Exit status 0 returned because command executed successfully.

lskdf      # Unrecognized command.
echo $?    # Non-zero exit status returned -- command failed to execute.


exit 113   # Will return 113 to shell.
           # To verify this, type "echo $?" after script terminates.

#  By convention, an 'exit 0' indicates success,
#+ while a non-zero exit value means an error or anomalous condition.
#  See the "Exit Codes With Special Meanings" appendix.
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.