Shebang - what's the point?

I've written a few hundred scripts now, and not once have I ever coded a
shebang line. I'm writing on both Windows and the Mac (Tiger).

Any time I'm kicking off a script from the Command Prompt or in Terminal
on the Mac, I simply type

ruby <name>.rb

and it works just fine.

So, when it comes down to it, what's the point of the shebang line?

Todd

···

--
Posted via http://www.ruby-forum.com/.

Basically so that instead of

ruby <name>.rb

you can write

./<name>.rb

in the shell prompt.

Will only work in *NIX environments though (UNIX, LINUX, BSD, OS X, Cygwin, etc.)

Cheers,

Enrique Comba Riepenhausen

···

On 11 May 2007, at 16:43, Todd Burch wrote:

I've written a few hundred scripts now, and not once have I ever coded a
shebang line. I'm writing on both Windows and the Mac (Tiger).

Any time I'm kicking off a script from the Command Prompt or in Terminal
on the Mac, I simply type

ruby <name>.rb

and it works just fine.

So, when it comes down to it, what's the point of the shebang line?

Todd

The shebang line makes scripts look and act similarly to regular executables.

···

On 5/11/07, Todd Burch <promos@burchwoodusa.com> wrote:

when it comes down to it, what's the point of the shebang line?

--
Greg Donald
http://destiney.com/

I've written a few hundred scripts now, and not once have I ever coded a
shebang line. I'm writing on both Windows and the Mac (Tiger).

Any time I'm kicking off a script from the Command Prompt or in Terminal
on the Mac, I simply type

ruby <name>.rb

and it works just fine.

So, when it comes down to it, what's the point of the shebang line?

Unix/Linux and the Apache webserver (among others) use it to determine
how to execute the file. If you set the file to be executable (via
chmod), and it has the correct shebang line, you wouldn't need to type
'ruby', you could run it with just the filename.

···

On 5/11/07, Todd Burch <promos@burchwoodusa.com> wrote:

Todd

--
Posted via http://www.ruby-forum.com/\.

--
Bill Guindon (aka aGorilla)
The best answer to most questions is "it depends".

Todd Burch wrote:

Any time I'm kicking off a script from the Command Prompt or in Terminal
on the Mac, I simply type

ruby <name>.rb

and it works just fine.

So, when it comes down to it, what's the point of the shebang line?

The point of the shebang line is not to have to tell the computer to open
the file with ruby. So you could write ./script or double click the script
in the file manager, even if it doesn't have the .rb-extension or the file
manager doesn't know how to open .rb-files.

···

--
Ist so, weil ist so
Bleibt so, weil war so

So, when it comes down to it, what's the point of the shebang line?

To summarize the to previous replies (Enrique and Greg):

Say you have a script that does some kind of sorting. You rename your

sort.rb file to sort

and give it the shebang line. If it's in the PATH you can simply run it
like:

$ sort testfile.txt

which I think is pretty neat.

As Enrique said before only *NIX environments support that.

Cheers,

Christian Luginbuehl

Todd Burch <promos@burchwoodusa.com> writes:

So, when it comes down to it, what's the point of the shebang line?

There's one more advantage to using a #! line that applies when you're
posting a script in an email message -- in that case, you should begin
your script with

#!ruby

and end it with

__END__

If you do that, then people can save your email message to a file and
just run it with:

ruby -x email_message.txt

Regardless of what kind of headers or footers got attached to the
email message. (So long as email didn't word-wrap your script,
everything should be good)

···

--
s=%q( Daniel Martin -- martin@snowplow.org
       puts "s=%q(#{s})",s.to_a.last )
       puts "s=%q(#{s})",s.to_a.last

I've always heard a slightly different, and in my opinion, logical
explanation.

Lets say you just got a new job as a sysadmin. The previous sysadmin was a
Perl guy, and since he was a good sysadmin, he used the shebang line in all
of his scripts , and he or she didn't add on the .pl .

Now, you come in, and seeing this Perl code, you scream and absolve to rewrite
it in Ruby, the cool kid on the block. After obtaining permission, you set
in and rewrite everything, and everything keeps working.

Now, imagine the same scenario, but no shebang line, and a .pl extension. If
you change the scripts, they won't work, because somewhere, someone is
running them with "perl myscript.pl", and myscript.pl will no longer exist,
or be a Perl script, after you are done with it.

So, shebang lines (and a lack of file extensions) are language-independent,
and future proof.

-Ben

···

On Friday, May 11 2007, Todd Burch wrote:

So, when it comes down to it, what's the point of the shebang line?

Todd,

People have posted valid reasons for using the shebang line, but
frankly I'm glad you brought this up, because I think a lot of
obsessive-compulsive programmers (myself included) write this when
it's really not necessary. If you're just learning ruby, writing a
one-off script, or writing a unit test script, it's perfectly fine to
leave it off. It takes time to write it and you usually have to jump
out of your editor to set the permissions to make it executable.

I think one of my favorite quotes, "A foolish consistency is the
hobgoblin of little minds", by Ralph Waldo Emerson applies here. Just
because it's useful in some situations isn't a reason to apply it to
all situations. If you're using a script/program that has the .rb file
extension and always going to be executed by typing "ruby", not
worrying about the shebang line saves you from:

1) Writing the shebang line
2) Deciding whether to use /usr/bin/ruby, /usr/bin/env ruby, or something else
3) Making the file executable
4) Obsessively-compulsively revisiting #2 next time you look at the file

Just my 2 cents.

-- Ben

···

On 5/11/07, Todd Burch <promos@burchwoodusa.com> wrote:

I've written a few hundred scripts now, and not once have I ever coded a
shebang line. I'm writing on both Windows and the Mac (Tiger).

Any time I'm kicking off a script from the Command Prompt or in Terminal
on the Mac, I simply type

ruby <name>.rb

and it works just fine.

So, when it comes down to it, what's the point of the shebang line?

Todd

--
Posted via http://www.ruby-forum.com/\.

--
Ben Atkin
ben@benatkin.com

OK, that all makes perfect sense. I figured there was a point, but in
my workflow, (work habits), I was not leveraging it.

Like my grandfather used to say - "if a man can't learn nothin' - he
ain't got no sense."

I learned something today! Thanks!
Todd

···

--
Posted via http://www.ruby-forum.com/.

Daniel Martin wrote:

There's one more advantage to using a #! line that applies when you're
posting a script in an email message -- in that case, you should begin
your script with

#!ruby

and end it with

__END__

If you do that, then people can save your email message to a file and
just run it with:

ruby -x email_message.txt

Regardless of what kind of headers or footers got attached to the
email message. (So long as email didn't word-wrap your script,
everything should be good)

I had absolutely no idea you could do that! How handy!!

Thanks!

···

--
Posted via http://www.ruby-forum.com/\.

Benjamin Kudria wrote:

So, shebang lines (and a lack of file extensions) are
language-independent,
and future proof.

-Ben

An excellent reason to have them!

Todd

···

--
Posted via http://www.ruby-forum.com/\.