Username Username - 6 months ago 13
Ruby Question

How do I use Bash to get a Ruby variable?

I have a Ruby script called

script.rb
, which looks like this:

puts 16


I have a Bash script that looks like this:

#!/bin/bash
SOME_VARIABLE=$(ruby script.rb)


I want
SOME_VARIABLE
to be equal to
16
, or whatever else I decide to
puts
from
script.rb
.

How do I do this?

Answer

You can use it as such.

#!/bin/bash
SOME_VARIABLE=`ruby script.rb`

If for example you want to export an environment variable.

export SOME_ENV=`ruby script.rb`
$ echo $SOME_ENV
16

More information on command substitution.

Comments