1

I am using Mac OS X. I have two versions (2.1.5 and 2.0.0) of Ruby installed. The former installed at /another/.path/to/ruby (there is a dot before "path" to mimic the fact that the path contains a dot-headed directory in between), in addition to the default system one (version 2.0.0) at /usr/bin/ruby. I used rbenv to install Ruby.

After I manually set the PATH environment variable so the default ruby command will be found in another directory: /another/.path/to/ruby. Now I check

which -a ruby

It is using correct ruby first, as output.

/another/.path/to/ruby
/usr/bin/ruby

Now I create a script, rbs, with the first line of shebang specifying the ruby to use.

#!/usr/bin/env ruby
puts 'hey there'

Then I run

./rbs

it outputs 'hey there'. Good. Meanwhile, the Ruby is using the correct version.

/usr/bin/env ruby --version

as well as

ruby --version

both output 2.1.5. So it does great job to use the new version.

However, here is where the problem occurs: now I update rbs file to be:

#!/another/.path/to/ruby
puts 'hey there'

Note that I updated the shebang to use the absolute path to the desired ruby. then I run

./rbs

It outputs:

./rbs: line 2: puts: command not found

which is too weird;

but if I run

ruby ./rbs

it outputs 'hey there' as normal. It looks like the shebang works perfect using /usr/bin/env ruby, but not for absolute path for the newly install ruby?

Why is this? Is there a way to fix it so the updated script can still work by typing the following?

./rbs

Thanks!

3
  • What is ./rubyscript in your ./rbs output? Commented Jan 7, 2015 at 3:25
  • Sorry, a typo. I will update it. It is just the ./rbs file. Commented Jan 7, 2015 at 3:27
  • 1
    Does /another/.path/to/ruby rbs work? Commented Jan 7, 2015 at 4:12

2 Answers 2

8

The puts: command not found message indicates that your script is not being run by Ruby, but by the shell instead. So first, I would double-check your shebang line's syntax and path.

Second, note that rbenv uses shims that dynamically find and run the right version of ruby (and associated programs like gem, etc). But the shims are scripts, and scripts can't themselves be shebang interpreters; you have to find and use the actual path to the ruby executable (as output by rbenv which ruby).

On the other hand, since /usr/bin/env is an executable, you can always use something like #!/usr/bin/env ruby, which will work even if the ruby it finds in the path is itself a script.

Sign up to request clarification or add additional context in comments.

1 Comment

This is an excellent answer! Thanks, Mark. Your point that shims are scripts that can't themselves be shebang interpreters makes the perfect sense.
0

I can't comment, (otherwise I'd add as a comment) but I think its worthwhile to add that the

#!/usr/bin/env ruby

MUST be the first line of the file. This tripped me up for a while.

source

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.