troubleshooting Question

bash. detecting last command exit code

Avatar of dportabella
dportabella asked on
Shell Scripting
2 Comments1 Solution9880 ViewsLast Modified:
A command returns a code after its execution, typically 0 if it went ok, or a code > 0 otherwise.

In bash, how to detect the exit code of the previous command, and assign it to a variable?
Something like:
lastExitCode=__SOMETHING__

Later in the script, how to detect if lastExitCode > 5 (for instance)
Something like:

if $lastExitCode > 5
  echo Hello
else
  echo Bye
end
 
ASKER CERTIFIED SOLUTION
m1tk4

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros