Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux - Capture exit code of a ruby script [duplicate]

Tags:

linux

bash

ruby

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

like image 474
dopplesoldner Avatar asked Nov 24 '25 03:11

dopplesoldner


2 Answers

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.

like image 142
Alfe Avatar answered Nov 26 '25 17:11

Alfe


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

like image 32
SBI Avatar answered Nov 26 '25 16:11

SBI