How Do You Find the Most Recent Exit Code in Linux?

Problem scenario
When a command stops, it returns an exit code. You want to learn more about exit codes starting with the variable that stores the most recently completed Linux command's exit code.  Every time you use $? in Linux, you get 0. Why is this?

There are four links that can help you learn more.  Two are from The Linux Documentation Project (one here and another here). has one.  And RedHat's article may still be noted as "unverified."  You do not need to go to any of these four.  

The Bash built-in variable $? stores the last exit status code.  It pertains to a process that has completed.  When you run commands successfully, the exit code is 0.

You could try to run this [garbage] command:


Then try this command twice:

exit $?

# The second time should return a zero because the exit code associated with "exit $?" should have been a "0".

You could also run this script:

echo "This script helps you learn about exit codes."
part1="try this command 'echo $"
part2="?' two or more times"
echo $part1$part2
exit 1

#Follow what the output of the script says.

Leave a comment

Your email address will not be published. Required fields are marked *