2008-12-03 15:14:21

by Geralt

[permalink] [raw]
Subject: Shebang - why are parameters not splitted on whitespace?

Hi,

I've just noticed, that it's not possible to use more than one
parameter in a shebang on linux, because everything following the
executable path in the shebang is treated as one parameter, so for
example
#!/bin/awk --re-interval -f
in a script called "test.awk" results in a call likes this:
/bin/awk "--re-interval -f" ./test.awk
when running it with "./test.awk".

Is there any reason why the parameters are not split before passing
them on to the program?
I mean we're talking here about linux where every program starts
somewhere with int main( int argc, char* argv[]), i.e. the arguments
are given piecewise, not in one big string (as it is the case for
win32 applications), so I was very surprised at first why my snipped
from above did not work and I suppose a lot of people would expect it
to behave in the natural way (= splitting the arguments).




Geralt.


2008-12-03 15:57:00

by Cong Wang

[permalink] [raw]
Subject: Re: Shebang - why are parameters not splitted on whitespace?

On Wed, Dec 3, 2008 at 3:14 PM, Geralt <[email protected]> wrote:
> Hi,
>
> I've just noticed, that it's not possible to use more than one
> parameter in a shebang on linux, because everything following the
> executable path in the shebang is treated as one parameter, so for
> example
> #!/bin/awk --re-interval -f
> in a script called "test.awk" results in a call likes this:
> /bin/awk "--re-interval -f" ./test.awk
> when running it with "./test.awk".
>
> Is there any reason why the parameters are not split before passing
> them on to the program?


Hello,

I think this behavior is different on different platforms, you can
find some clues
in man page of execve(2):

The semantics of the optional-arg argument of an interpreter script
vary across implementations. On Linux, the entire string following the
interpreter name is passed as a single argument to the interpreter, and
this string can include white space. However, behavior differs on some
other systems. Some systems use the first white space to terminate
optional-arg. On some systems, an interpreter script can have multiple
arguments, and white spaces in optional-arg are used to delimit the
arguments

It seems that POSIX doesn't specify this.

Thanks.

2008-12-03 17:30:11

by Geralt

[permalink] [raw]
Subject: Re: Shebang - why are parameters not splitted on whitespace?

On Wed, Dec 3, 2008 at 4:56 PM, Am?rico Wang <[email protected]> wrote:
> On Wed, Dec 3, 2008 at 3:14 PM, Geralt <[email protected]> wrote:
>> Hi,
>>
>> I've just noticed, that it's not possible to use more than one
>> parameter in a shebang on linux, because everything following the
>> executable path in the shebang is treated as one parameter, so for
>> example
>> #!/bin/awk --re-interval -f
>> in a script called "test.awk" results in a call likes this:
>> /bin/awk "--re-interval -f" ./test.awk
>> when running it with "./test.awk".
>>
>> Is there any reason why the parameters are not split before passing
>> them on to the program?
>
>
> Hello,
>
> I think this behavior is different on different platforms, you can
> find some clues
> in man page of execve(2):
>
> The semantics of the optional-arg argument of an interpreter script
> vary across implementations. On Linux, the entire string following the
> interpreter name is passed as a single argument to the interpreter, and
> this string can include white space. However, behavior differs on some
> other systems. Some systems use the first white space to terminate
> optional-arg. On some systems, an interpreter script can have multiple
> arguments, and white spaces in optional-arg are used to delimit the
> arguments
>
> It seems that POSIX doesn't specify this.
>
> Thanks.
>

Hello,

I should have anticipated this objection :-). I'm aware that this is
not consistent among Unix operating systems (you can find more about
it here [1]), but I was specifically refering to linux in this case
and why it handles the shebang arguments as one big string and not as
single arguments for the application.



Geralt.


[1] http://www.in-ulm.de/~mascheck/various/shebang/

2008-12-06 01:05:06

by Cong Wang

[permalink] [raw]
Subject: Re: Shebang - why are parameters not splitted on whitespace?

On Wed, Dec 3, 2008 at 5:29 PM, Geralt <[email protected]> wrote:
> Hello,
>
> I should have anticipated this objection :-). I'm aware that this is
> not consistent among Unix operating systems (you can find more about
> it here [1]), but I was specifically refering to linux in this case
> and why it handles the shebang arguments as one big string and not as
> single arguments for the application.

I can't think out any strong reasons, maybe only becasue Linux developers
prefer that approach.

Thanks.

2008-12-06 05:41:36

by Valdis Klētnieks

[permalink] [raw]
Subject: Re: Shebang - why are parameters not splitted on whitespace?

On Sat, 06 Dec 2008 01:04:52 GMT, Am?rico Wang said:
> On Wed, Dec 3, 2008 at 5:29 PM, Geralt <[email protected]> wrote:
> > Hello,
> >
> > I should have anticipated this objection :-). I'm aware that this is
> > not consistent among Unix operating systems (you can find more about
> > it here [1]), but I was specifically refering to linux in this case
> > and why it handles the shebang arguments as one big string and not as
> > single arguments for the application.
>
> I can't think out any strong reasons, maybe only becasue Linux developers
> prefer that approach.

And of course, "whitespace" is a somewhat fungible concept. 'man bash'
and read about $IFS.


Attachments:
(No filename) (226.00 B)

2008-12-06 12:02:25

by Geralt

[permalink] [raw]
Subject: Re: Shebang - why are parameters not splitted on whitespace?

On Sat, Dec 6, 2008 at 6:41 AM, <[email protected]> wrote:
> On Sat, 06 Dec 2008 01:04:52 GMT, Am?rico Wang said:
>> On Wed, Dec 3, 2008 at 5:29 PM, Geralt <[email protected]> wrote:
>> > Hello,
>> >
>> > I should have anticipated this objection :-). I'm aware that this is
>> > not consistent among Unix operating systems (you can find more about
>> > it here [1]), but I was specifically refering to linux in this case
>> > and why it handles the shebang arguments as one big string and not as
>> > single arguments for the application.
>>
>> I can't think out any strong reasons, maybe only becasue Linux developers
>> prefer that approach.
>
> And of course, "whitespace" is a somewhat fungible concept. 'man bash'
> and read about $IFS.
>
So you're saying that you can't do that because you don't know where
to split the parameters, right? But would it be that bad to state a
rule what "whitespace" is in the shebang line?

2008-12-06 21:15:01

by Aaron Cohen

[permalink] [raw]
Subject: Re: Shebang - why are parameters not splitted on whitespace?

You should read this thread:
http://unix.derkeiler.com/Mailing-Lists/FreeBSD/arch/2005-02/0039.html

The shebang processing needs to somehow magically split up the first
line into the file into "some number of arguments for the interpreter"
"some number of arguments for the script being run", and "the script
being run."

The first two are the big difficulty, how should the kernel know which
arguments are meant for the interpreter and which are meant for the
script?

In the article I referenced, current Linux behavior seems to conform
to at least the majority of other OSes do, and I believe freeBSD
actually moved from trying to split the arguments up into something
similar (though I couldn't swear to it).

--Aaron

On Sat, Dec 6, 2008 at 4:02 AM, Geralt <[email protected]> wrote:
> On Sat, Dec 6, 2008 at 6:41 AM, <[email protected]> wrote:
>> On Sat, 06 Dec 2008 01:04:52 GMT, Am?rico Wang said:
>>> On Wed, Dec 3, 2008 at 5:29 PM, Geralt <[email protected]> wrote:
>>> > Hello,
>>> >
>>> > I should have anticipated this objection :-). I'm aware that this is
>>> > not consistent among Unix operating systems (you can find more about
>>> > it here [1]), but I was specifically refering to linux in this case
>>> > and why it handles the shebang arguments as one big string and not as
>>> > single arguments for the application.
>>>
>>> I can't think out any strong reasons, maybe only becasue Linux developers
>>> prefer that approach.
>>
>> And of course, "whitespace" is a somewhat fungible concept. 'man bash'
>> and read about $IFS.
>>
> So you're saying that you can't do that because you don't know where
> to split the parameters, right? But would it be that bad to state a
> rule what "whitespace" is in the shebang line?
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to [email protected]
> More majordomo info at http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at http://www.tux.org/lkml/
>