Sign up ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

This question already has an answer here:

I have a simple ruby script which uses the abort function to exit with a non-zero exit code

#!/usr/bin/env ruby

puts "I ran"
abort "Exiting"

How can I capture the exit code when I execute this command in bash?

I have tried exit_code=./test or exit_code=ruby test to no avail.

Thanks

share|improve this question

marked as duplicate by Alfe, devnull, legoscia, fedorqui, ling.s Mar 1 '14 at 4:21

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
must be a duplicate ;-) –  Alfe Oct 30 '13 at 10:51

3 Answers 3

up vote 2 down vote accepted

Try this:

./test
echo $?

The special shell variable $? contains the exit code of the last terminated program.

It does not matter whether your program is a ruby program. All Unix programs have an exit code which is handled alike in the starting shell.

share|improve this answer

You find the exit code from the previously executed command in the variable $?.

share|improve this answer

The exit code of the last program that ran is stored in $?

share|improve this answer

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