I'm on OS X (with bash) and a newbie at unix. I want to know if it's possible to amend some file such that to run a ruby program, I don't need "ruby file.rb", but instead can just run "ruby.rb".
Is there a reason NOT to do this?
Thanks!
As others have mentioned, you want to have a shebang (#!
) line at the beginning, and change the permissions to executable.
I would recommend using #!/usr/bin/env ruby
instead of the path to Ruby directly, since it will make your script more portable to systems that may have Ruby installed in different directories; env
will search in your search path, and so it will find the same Ruby that you would execute if you ran ruby
on the command line. Of course, this will have problems if env
is in a different location, but it is much more common for env
to be at /usr/bin/env
than for Ruby to be at /usr/bin/ruby
(it may be in /usr/local/bin/ruby
, /opt/bin/ruby
, /opt/local/bin/ruby
, etc)
#!/usr/bin/env ruby
puts "Hello!"
And make it executable:
chmod +x file.rb
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With