Run Ruby code from within a shell script

If you need to run Ruby code from within a shell script, you could easily execute a Ruby script with a simple bash command. This means you will have two files – one bash script and one Ruby script.

However, what if you want to combine the two file into a single file?

Simple, using a heredoc in your bash script, we simply pass the entire heredoc to the ruby executable. Create a file called hybrid which contains the following code:

#!/usr/bin/env sh

echo "This is bash!"

/usr/bin/env ruby <<-EORUBY

  puts 'This is ruby!'

EORUBY

Running the above bash script will produce this:

$ . hybrid
This is bash!
This is ruby!

Hope this helps.

Advertisements

12 thoughts on “Run Ruby code from within a shell script

  1. I don’t know if you can access local variables from the bash side of the script, but you can use environment variables, making them accessible to both bash and ruby.

    Something like…

    # bash
    export MY_NAME=’ed’

    # ruby
    puts ENV[‘MY_NAME’]

    1. This is great! But is there anyways to pass a variable from the ruby back outside to bash? The ENV vars are working well for bash into ruby, but not back out to bash.

      Great article & reply 🙂

      1. Thanks. I really don’t know of a way to pass variables back from Ruby to Bash. So far I haven’t needed to do that. Let me know if you find a way to do it.

  2. “If you need to run Ruby code from within a shell script, you could easily execute a Ruby script with a simple bash command. This means you will have two files – one bash script and one Ruby script.”

    So actually, this is surprisingly *not* easy, and is why I’m here. The reason is that if you want to do it that way, the Bash script needs to know where the Ruby script lives, but this is a remarkably hard problem if you want to do it really nicely! (See http://mywiki.wooledge.org/BashFAQ/028 and mentally substitute “Ruby file” in for “configuration file”)

  3. Woah! I’m really digging the template/theme of this blog. It’s simple, yet effective.
    A lot of times it’s challenging to get that “perfect balance” between user friendliness and visual appearance. I must say you have done a superb job with this. Also, the blog loads very quick for me on Safari. Exceptional Blog!

  4. Excellent post. I was checking constantly this weblog and I
    am impressed! Extremely useful information specifically the closing phase 🙂 I maintain such info a lot.
    I was seeking this particular information for a very
    lengthy time. Thank you and best of luck.

  5. I really love your blog.. Excellent colors & theme.
    Did you develop this amazing site yourself? Please reply back as I’m looking to create my own personal website and would like to find out where you got this from or what the theme is named. Thank you!

  6. I have been exploring for a little bit for any high
    quality articles or weblog posts in this sort of area . Exploring in Yahoo I finally stumbled upon this site.
    Reading this info So i’m happy to convey that I have an incredibly excellent uncanny feeling I found out just what I needed. I most without a doubt will make certain to don?t fail to remember this website and provides it a look on a relentless basis.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s