Invocation of ruby interpreter for .rb files under bash

Z

Zouplaz

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
 
L

Lex Williams

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
 
M

Maciej Tomaka

Zouplaz said:
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
 
S

Stefano Crocco

Alle Thursday 25 September 2008, Zouplaz ha scritto:
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

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
 
Y

Yaser Sulaiman

[Note: parts of this message were removed to make it a legal post.]

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 Sulaiman
 
R

Randy Kramer

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.: /<whatever>/foo

(Just feeling pedantic, I guess.)

Randy Kramer
 
Z

Zouplaz

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 !
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top