2

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

dopplesoldner
  • 8,891
  • 12
  • 44
  • 55

3 Answers3

6

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.

Alfe
  • 56,346
  • 20
  • 107
  • 159
2

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

SBI
  • 2,322
  • 1
  • 17
  • 17
1

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

steffen
  • 16,138
  • 4
  • 42
  • 81