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,015
  • 12
  • 38
  • 54

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
  • 52,016
  • 18
  • 95
  • 150
2

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

SBI
  • 2,282
  • 1
  • 16
  • 17
1

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

steffen
  • 14,490
  • 3
  • 40
  • 77