In an executable file, you specify the environment using a shebang:

#!/usr/bin/env ruby

# ruby code here

But what if you want to specify multiple possible environments? For example, if you have multiple versions of Ruby installed, you may want to do something like


# ruby code here

How can you specify multiple possible environments for a single executable file?

2 Answers 2


The short answer is that you can't.

The idea behind using #!/usr/bin/env ruby instead of simply #!/usr/bin/ruby is to use whatever ruby binary is first in the user's path. Then your script doesn't depend on ruby being in the same place on every system; after all, a 'normal' ruby location might be /usr/bin/ruby, /usr/local/bin/ruby, /opt/ruby-1.8.7/bin/ruby, etc...

If you DID want to hardcode some values or do some searching logic, you could always make a wrapper shell script:


for ruby in /usr/bin/ruby /usr/local/bin/ruby
    if [ -x "$ruby" ]
        "$ruby" myscript.rb

And if you're sufficiently motivated, you could even embed a ruby (or other) script inside the shell script.

  • That makes sense. I plan to run this script on multiple machines, each of which has a different path to Ruby 1.9, and they don't all use Ruby 1.9 as the default interpreter. I'll try using the wrapper script. Thanks!
    – sffc
    Commented Aug 15, 2012 at 15:43
  • 1
    Don't forget to $@ when calling the final script to forward possible args: "$ruby" myscript.rb $@ Commented Dec 18, 2019 at 22:50

I'm going to contradict mrb and say that you can do alternates, without changing the "#!" line.

Suppose you have a ruby script with #!/usr/bin/env ruby as the first line.

You want /path/to/better/ruby/version/whatever to run the script, you set the PATH environment variable to have /path/to/better/ruby/version first:

PATH=/path/to/better/ruby/version:$PATH  whatever_script_name_is

You want /usr/local/bin/ruby to run the script? Set PATH appropriately:

PATH=/usr/local/bin:$PATH  whatever_script_name_is

Note that fiddling with PATH can cause other problems in the case that the ruby executable you want to run the script resides in the same directory with other alternate programs. For example, if /usr/local/bin had BSD cat, grep, sed in it as well as ruby, then when you set PATH=/usr/local/bin:$PATH you will get the BSD executables instead of Linux executables. That could case a problem.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .