diff --git a/README.md b/README.md index f26e688..a14ebb4 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://tinyletter.com/jbranchaud). -_1085 TILs and counting..._ +_1086 TILs and counting..._ --- @@ -839,6 +839,7 @@ _1085 TILs and counting..._ - [Assoc For Hashes](ruby/assoc-for-hashes.md) - [Block Comments](ruby/block-comments.md) - [Chaining Multiple RSpec Change Matchers](ruby/chaining-multiple-rspec-change-matchers.md) +- [Check Return Status Of Running A Shell Command](ruby/check-return-status-of-running-a-shell-command.md) - [Click On Text With Capybara](ruby/click-on-text-with-capybara.md) - [Colorful Output With MiniTest](ruby/colorful-output-with-minitest.md) - [Comparing Class Hierarchy Relationships](ruby/comparing-class-hierarchy-relationships.md) diff --git a/ruby/check-return-status-of-running-a-shell-command.md b/ruby/check-return-status-of-running-a-shell-command.md new file mode 100644 index 0000000..8a9933a --- /dev/null +++ b/ruby/check-return-status-of-running-a-shell-command.md @@ -0,0 +1,33 @@ +# Check Return Status Of Running A Shell Command + +There are [many +ways](http://tech.natemurray.com/2007/03/ruby-shell-commands.html) to run a +shell command in a Ruby context. One of the most common is to place the command +inside backticks. It runs the command in a subshell. + +The standard output (`stdout`) from running the command is the return value of +the statement. Ruby also captures details about the previously run command in +the global `$?` variable. + + +```ruby +> `true` +=> "" +> $?.exitstatus +=> 0 + +> `false` +=> "" +> $?.exitstatus +=> 1 + +> `echo 'hello' && exit 2` +=> "hello\n" +> $? +=> # +> $?.exitstatus +=> 2 +``` + +The value captured in `$?` is a `Process::Status` object. It can tell us the +exit status of the process with `#exitstatus`.