0

I want to know the ruby version being run from a bash script.

That can be done with ruby -v:

$ ruby -v
ruby 1.9.3p545 (2014-02-24) [i686-linux] Brightbox

My problem is that I just want the version number. How do I transform that output into this?

1.9.3p545
2
  • 3
    Is the p545 important? If not, you could do ruby -e 'puts RUBY_VERSION' Commented Sep 17, 2014 at 11:24
  • 1
    @TomFenech cool!. Without newline character ruby -e '$><<RUBY_VERSION' Commented Sep 17, 2014 at 11:33

3 Answers 3

3

Through cut,

$ ruby -v | cut -d" " -f2
1.9.3p484

Through awk,

$ ruby -v | awk '{print $2}'
1.9.3p484
Sign up to request clarification or add additional context in comments.

Comments

2

Try this:

ruby -v | cut -d' ' -f2

Comments

2

Some solutions use awk or cut, those waste a child process. Try this:

read p ver rest < <(ruby -v)
echo $ver

$p will contain ruby, and $rest the trailing fields. This solution only uses one child process (ruby), not two.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.