Skip to contentSkip to conversation

Shebang - why are parameters not splitted on whitespace?

DISCUSSION Geralt 4 participants 7 messages Dec 3, 2008 – Dec 6, 2008 1 message not in the archive

lore
Share & tools
lore

Geralt

author
#0 lore raw

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.

#1 lore raw
Quoted 11 lines · Geralt

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".

Quoted 2 lines

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.

A message that is not in the archive (sent off-list or not yet received): [email protected]

author
#3 lore raw
Quoted 31 lines · Américo Wang
Quoted 11 lines · Geralt

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".

Quoted 2 lines

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

Quoted 4 lines

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/

#4 lore raw
Quoted 7 lines · Geralt

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.

#5 lore raw
Quoted 11 lines · Américo Wang
Quoted 7 lines · Geralt

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.

author
#6 lore raw
Quoted 10 lines · On Sat, Dec 6, 2008 at 6:41 AM, <[email protected]> wrote:
Quoted 9 lines · Américo Wang
Quoted 7 lines · Geralt

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.

Quoted 6 lines
Quoted 2 lines

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?

#7 lore raw

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

Quoted 26 lines · Geralt
Quoted 10 lines · On Sat, Dec 6, 2008 at 6:41 AM, <[email protected]> wrote:
Quoted 9 lines · Américo Wang
Quoted 7 lines · Geralt

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.

Quoted 6 lines
Quoted 2 lines

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/