# Invocation of ruby interpreter for .rb files under bash

**URL:** <https://www.ruby-forum.com/t/invocation-of-ruby-interpreter-for-rb-files-under-bash/149500>\
**Category:** Ruby\
**Created:** [September 25, 2008, 1:45pm UTC](https://www.ruby-forum.com/t/invocation-of-ruby-interpreter-for-rb-files-under-bash/149500 "2008-09-25T13:45:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [September 25, 2008, 1:45pm UTC](https://www.ruby-forum.com/t/invocation-of-ruby-interpreter-for-rb-files-under-bash/149500/1 "2008-09-25T13:45:38Z")

</div>

Hello, this is not really related to the core language by itself but I  
don’t remember how to have the interpreter launched when typing foo.rb  
or foo (if foo is the name of the script) under bash ?

I remember that I have to put something like  
#! /usr/ruby

at the beginnin of the script but I don’t remember the right syntax and  
didn’t found a Google way to express it.

Thanks

---

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [September 25, 2008, 1:51pm UTC](https://www.ruby-forum.com/t/invocation-of-ruby-interpreter-for-rb-files-under-bash/149500/2 "2008-09-25T13:51:13Z")

</div>

the first line should be

#!/usr/bin/ruby

or wherever your script is.

Next, you have to mark the script as beeing executable .

chmod +x foo.rb

Note that since the first line of your script is #!/usr/bin/ruby ( or  
wherever your ruby binary is located ) , the script’s name doesn’t have  
to end with .rb.  
After that , you can just write :  
./foo

---

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [September 25, 2008, 1:51pm UTC](https://www.ruby-forum.com/t/invocation-of-ruby-interpreter-for-rb-files-under-bash/149500/3 "2008-09-25T13:51:25Z")

</div>

Zouplaz wrote:

> Hello, this is not really related to the core language by itself but I  
> don’t remember how to have the interpreter launched when typing foo.rb  
> or foo (if foo is the name of the script) under bash ?
> 
> I remember that I have to put something like  
> #! /usr/ruby
> 
> at the beginnin of the script but I don’t remember the right syntax and  
> didn’t found a Google way to express it.
> 
> Thanks  
> #!/usr/bin/ruby

to check the path please use: which ruby  
on my box it returned /usr/bin/ruby  
$ which ruby  
/usr/bin/ruby

Regards

---

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [September 25, 2008, 1:54pm UTC](https://www.ruby-forum.com/t/invocation-of-ruby-interpreter-for-rb-files-under-bash/149500/4 "2008-09-25T13:54:27Z")

</div>

Alle Thursday 25 September 2008, Zouplaz ha scritto:

> Thanks

You’re almost there. It’s #! followed by a space then the path of the  
ruby  
executable:

#! /usr/bin/ruby

Another option is this:

#! /usr/bin/env ruby

This way, you don’t need to know the position of the ruby executable,  
but only  
of the env executable (which, I suppose, should be more standard). The  
downside is that, at least with bash, you can’t pass options to ruby.  
For  
example, the line

#! /usr/bin/env ruby -w

tries to get from env the path of the program ruby -w. Since ruby -w is  
not a  
program, you get an error.

In the first way, instead, you can pass options to ruby:

#! /usr/bin/ruby -w

Stefano

---

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [September 25, 2008, 5:03pm UTC](https://www.ruby-forum.com/t/invocation-of-ruby-interpreter-for-rb-files-under-bash/149500/5 "2008-09-25T17:03:00Z")

</div>

On Thursday 25 September 2008 07:50 am, Lex W. wrote:

> the first line should be  
> #!/usr/bin/ruby  
> or wherever your script is.

^ s/script/ruby interpreter/

> Next, you have to mark the script as beeing executable .
> 
> chmod +x foo.rb
> 
> Note that since the first line of your script is #!/usr/bin/ruby ( or  
> wherever your ruby binary is located ) , the script’s name doesn’t  
> have  
> to end with .rb.

> After that , you can just write :  
> ./foo

… assuming (I know) you are “in” the directory where the script is.

Alternatively, you can:

- put the script in a directory on the path that Linux searches for  
executables (then just type foo) (run =set | grep PATH=)
- change the path which Linux searches for executables to include the  
path where your script is located (again, run =set | grep PATH=)
- include the full path to the executable on the command line,  
e.g.: //foo

(Just feeling pedantic, I guess.)

Randy K.

---

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [September 25, 2008, 2:38pm UTC](https://www.ruby-forum.com/t/invocation-of-ruby-interpreter-for-rb-files-under-bash/149500/6 "2008-09-25T14:38:05Z")

</div>

You should put the following line at the beginning of the script:  
#!/usr/bin/env ruby

Then, you should make your script executable using the chmod command:  
Prefacechmod +x foo.rb

Now, you should be able to run the script by typing:  
./foo.rb

Regards,  
Yaser S.

---

<div class="post-metadata">

**Author:** ![Zouplaz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/a4c791/32.png) [@Zouplaz](https://www.ruby-forum.com/u/Zouplaz)\
**Post date:** [September 26, 2008, 10:16am UTC](https://www.ruby-forum.com/t/invocation-of-ruby-interpreter-for-rb-files-under-bash/149500/7 "2008-09-26T10:16:03Z")

</div>

le 25/09/2008 13:42, Zouplaz nous a dit:

> Hello, this is not really related to the core language by itself but I  
> don’t remember how to have the interpreter launched when typing foo.rb  
> or foo (if foo is the name of the script) under bash ?
> 
> I remember that I have to put something like  
> #! /usr/ruby
> 
> at the beginnin of the script but I don’t remember the right syntax and  
> didn’t found a Google way to express it.

Thanks all for you help, now the script files are executed the right way  
!
