PDA

View Full Version : What's the Difference Between #!/bin/bash and #/bin/bash ???



user1397
October 13th, 2006, 08:56 PM
does that exclamation point have to be there?

iovar
October 13th, 2006, 09:09 PM
The # is just for commenting. When you add the ! you have a shebang line (http://en.wikipedia.org/wiki/Shebang_(Unix))
which generally means, that if the file is made into an executable,
that line will be executed first.

prizrak
October 13th, 2006, 09:26 PM
The ! is scarier ;) But yeah what the dude above me said :)

user1397
October 13th, 2006, 09:56 PM
The # is just for commenting. When you add the ! you have a shebang line (http://en.wikipedia.org/wiki/Shebang_%28Unix%29)
which generally means, that if the file is made into an executable,
that line will be executed first.so if you dont have the !, what happens?

mostwanted
October 13th, 2006, 10:00 PM
so if you dont have the !, what happens?

Nothing, it's just a comment.

skymt
October 13th, 2006, 10:04 PM
Nothing, it's just a comment.

No, it isn't. The system needs a shebang line (#!/bin/bash) in order to know how to run the file. Without it, it wouldn't know whether it's Perl, Python, or Awk. It doesn't work without the #, either. And not just because of Bash, either. The system needs the #.

You don't need the shebang line if you run it as "bash myscript", but you need it if you want to just type "myscript".

EDIT: I just did a rather cool demo of this. Make a file, and put this in it:
#!/usr/bin/gedit
This file will open in gEdit.Make it executable (chmod +x filename) and run it (./filename). It will open in gEdit.

mostwanted
October 13th, 2006, 10:14 PM
No, it isn't. The system needs a shebang line (#!/bin/bash) in order to know how to run the file. Without it, it wouldn't know whether it's Perl, Python, or Awk. It doesn't work without the #, either. And not just because of Bash, either. The system needs the #.

He was asking whether something would happen if the ! was missing to which I would replied "nothing happens" because then it would just be a comment and not an execution path. I don't really know where you're going at with this long reply...

user1397
October 13th, 2006, 10:45 PM
i see...then how come aysiu's songbird script does not include it?

http://www.psychocats.net/ubuntu/songbird

meng
October 13th, 2006, 10:49 PM
Hey that's a real good question! From what I thought I knew, that shouldn't work, unless one specifically runs
sh songbird.sh
but maybe someone can confirm if it runs as-is?

skymt
October 13th, 2006, 10:50 PM
He was asking whether something would happen if the ! was missing to which I would replied "nothing happens" because then it would just be a comment and not an execution path. I don't really know where you're going at with this long reply...

I misunderstood his post. I thought he was asking what would happen if you left out the whole ! line. I was trying to clarify. Sorry. :oops:

But you have to admit, the gEdit thing is kind of cool. ;)

givré
October 14th, 2006, 12:00 AM
It's an error.
#/bin/bash will be take as a comment, and you can put #/bin/what_the_hell or what you want it will change nothing.
If the script is executed, it's because you launch it from an interactive shell, and so the script will be executed as a shell script.
Removing it will also have the same effect.
But if you execute it from an external program, it will not work. That's why you need to shebang to #!/bin/sh or #!/bin/bash at the beginning of a shell script.
#/bin/bash means nothing.

asimon
October 14th, 2006, 12:04 AM
Hey that's a real good question! From what I thought I knew, that shouldn't work, unless one specifically runs
sh songbird.sh
but maybe someone can confirm if it runs as-is?
Yes, it works without the shebang too.
From bash's manual:


When a normal program is executed, the shell runs the program, passing the arguments and the environment to the program. If the program is not a normal executable file (i.e., if it does not begin with the "magic number" whose ASCII representation is "#!", so execve(2) returns ENOEXEC then) the shell will interpret the program in a subshell.
Thus bash will interpret the songbird.sh without a shebang a bash script. Other shells may do other things.

meng
October 14th, 2006, 12:28 AM
Thanks for clarifying! This is what I love about the community.

user1397
October 14th, 2006, 01:52 AM
why go thru that trouble? (well its not rally trouble, but..)
i mean, why would you ever want to write #/bin/bash instead of #!/bin/bash? does not having the ! really have a purpose at all?

givré
October 14th, 2006, 09:20 AM
It's just a mistake, #/bin/bash don't have a purpose at all.

user1397
October 14th, 2006, 03:47 PM
It's just a mistake, #/bin/bash don't have a purpose at all.ah, ok, thanks for all the knowledge