Search code examples
rubybackticks

What does $? mean in Ruby?


I just read the comparison of PHP, Perl, Ruby and Python

http://hyperpolyglot.org/scripting

and saw the following code sample.

files = `ls -l /tmp`
unless $?.success?
  raise "ls failed"
end

files = %x(ls)
unless $?.success?
   raise "ls failed"
end

I was wondering what $? stands for....


Solution

  • $? contains the last executed command's exit code. Which can be accessed as $?.exitstatus