#! vs : use ?

#! vs : use ?

am 26.01.2008 21:45:07 von strombrg

Almost all *ix and even Cygwin appears to do #! now.

But ISTR that long ago, #! was mostly a BSD-ism, and SysV instead
preferred something like ": use /bin/sh".

Is that right?

And if ": use /bin/sh" is (was) correct, would a file whose first line
starts with just ":" be a shorthand for "run me with Bourne shell"?

We have some files that start with just a colon on the first line. I
tested it on an Ubuntu Linux system, and it doesn't really seem to be
doing anything, but would this have been the intent?

Thanks!

Re: #! vs : use ?

am 26.01.2008 22:21:18 von Stephane CHAZELAS

On Sat, 26 Jan 2008 20:45:07 GMT, Dan Stromberg wrote:
>
> Almost all *ix and even Cygwin appears to do #! now.
>
> But ISTR that long ago, #! was mostly a BSD-ism, and SysV instead
> preferred something like ": use /bin/sh".
>
> Is that right?
>
> And if ": use /bin/sh" is (was) correct, would a file whose first line
> starts with just ":" be a shorthand for "run me with Bourne shell"?
>
> We have some files that start with just a colon on the first line. I
> tested it on an Ubuntu Linux system, and it doesn't really seem to be
> doing anything, but would this have been the intent?
[...]

: happens to be something different from #. That's the only
things that matters (and that it is a command that does
nothing).

In the early days, it wasn't the system that was executing an
interpreter when a file was starting with "#!". Instead, it was
the shell.

csh was the first one to have that idea. So, if, in the csh
shell, you were executing a file and the system returned ENOEXE
(not a recognised executable), csh would try and interpret it.

At that time, it happened that only csh was recognising "#" as
the start of a comment. So, csh used that to decide when to try
and interprete a file upon a ENOEXEC. So if a file started with
"#" it was a csh script, otherwise something else (in which case
I'm not sure whether it was trying to run it with sh).

So, in those times, # was meaning csh (at least for csh users).

Later on, that feature evolved and was moved to the kernel.

Now, we have the #! line which is handled slightly differently
in every system, but is recognized by every ones.

But, it is not POSIX. Instead POSIX says that in a POSIX
environment, sh and execp..., upon a ENOEXEC are to have the
file interpreted by a POSIX sh unless the first line starts with
#! in which case the behavior is unspecified.

So, nowadays, if you want to have a file interpreted by a
specific interpreter, use

#! /path/to/interpreter -
(assuming that that interpreter is able to discard that first
line as being a comment for instance).

That won't be POSIX but will work as long as the path is not too
long.

But, if you want to write a POSIX script, you should avoid
starting your file with #!.

That could be

### HERE'S A POSIX SCRIPT, MAKE SURE TO CALL IT IN A POSIX
### ENVIRONMENT


: let us hope we're called in a POSIX environment

....

--
Stephane

Re: #! vs : use ?

am 27.01.2008 12:34:36 von Sven Mascheck

Stephane Chazelas wrote:

> So if a file started with "#" it was a csh script,
> otherwise something else [...]

In the csh source "/bin/sh" is hardcoded as macro to be execed.

> So, in those times, # was meaning csh (at least for csh users).

....and anything else meant "not csh", that is, "sh".
And the only way to comment in the original sh versions was
the null command ":". The colon itself never had any further meaning.

Only on BSDs, sh was hacked symmetrically: Upon ENOEXEC
and '#' as first character, "/bin/csh" was execed.

> Later on, that feature evolved and was moved to the kernel.

This can suggest that #! was derived from #.
But # and #! seem to be quite independent issues.
The csh/sh-hack only existed on BSDs, where csh was available.
The #! mechanism in contrast was invented at Bell Labs;
Dennis Ritchie mailed the code also to Berkeley,
and so it was first available to the public on 4BSD...

--

Re: #! vs : use ?

am 28.01.2008 23:24:34 von Dan Stromberg

On Sat, 26 Jan 2008 21:21:18 +0000, Stephane Chazelas wrote:

> On Sat, 26 Jan 2008 20:45:07 GMT, Dan Stromberg wrote:
>>
>> Almost all *ix and even Cygwin appears to do #! now.
>>
>> But ISTR that long ago, #! was mostly a BSD-ism, and SysV instead
>> preferred something like ": use /bin/sh".
>>
>> Is that right?
>>
>> And if ": use /bin/sh" is (was) correct, would a file whose first line
>> starts with just ":" be a shorthand for "run me with Bourne shell"?
>>
>> We have some files that start with just a colon on the first line. I
>> tested it on an Ubuntu Linux system, and it doesn't really seem to be
>> doing anything, but would this have been the intent?
> [...]
>
> : happens to be something different from #. That's the only things that
> matters (and that it is a command that does nothing).

Yes, colon is basically an evaluated commented - a shell builtin for /bin/
true.

But I believe it also had a #!-like role on some systems. I'm pulling it
from my memory, but also, there's something about checking for ": use" as
an alternative to #! in the perl (sigh, not my favorite language, but the
Configure script knows a lot about *ix history) Configure script.

> In the early days, it wasn't the system that was executing an
> interpreter when a file was starting with "#!". Instead, it was the
> shell.

Agreed.

Is there anyone around who remembers the specifics of ": use" on the
first line - and if just ":" would have a special meaning? I'm still
thinking it was probably a SysV-ism. I have no guess as to whether it
was in-kernel or in-execing-shell, though perhaps it does seem a little
long to cram into a 16 bit (?) magic number.

Re: #! vs : use ?

am 29.01.2008 10:19:33 von Stephane CHAZELAS

On Mon, 28 Jan 2008 22:24:34 GMT, Dan Stromberg wrote:
[...]
>> : happens to be something different from #. That's the only things that
>> matters (and that it is a command that does nothing).
>
> Yes, colon is basically an evaluated commented - a shell builtin for /bin/
> true.
>
> But I believe it also had a #!-like role on some systems. I'm pulling it
> from my memory, but also, there's something about checking for ": use" as
> an alternative to #! in the perl (sigh, not my favorite language, but the
> Configure script knows a lot about *ix history) Configure script.

Looks to me that that ": use" in that script is intended for a
human, not a machine, when machines (the system or shell)
doesn't recognise any shebang mechanism.

Note that perl parses the shebang line (to overcome system
limitations), and it doesn't recognise the ": use" line in that
case, which I think is a confirmation that the ": use" in the
Configure script is not considered as something that would be
interpreted by a machine.


>> In the early days, it wasn't the system that was executing an
>> interpreter when a file was starting with "#!". Instead, it was the
>> shell.
>
> Agreed.
[...]

From what Sven said, it's probably wrong. The shell (csh
initially) was making a difference between a file starting with
"#" and one starting with anything else, but it would seem that
#! (a SysV thing) was first introduced in the kernel, and that
some shells later on, may have added it for when they were being
used on systems that didn't support #!.

--
Stephane