[Slackbuilds-users] bashisms?

Andrzej Telszewski atelszewski at gmail.com
Wed Sep 2 15:09:45 UTC 2015


On 02/09/15 16:51, John Vogel wrote:
> On Wed, 2 Sep 2015 15:52:30 +0200
> Andrzej Telszewski <atelszewski at gmail.com> wrote:
>
>> Hi,
>>
>> Today I was writing code to execute script from C code and I'd like to
>> share with you what I've found. It stroke me, but should otherwise be
>> obvious.
>>
>> It seems that shebang has only meaning if the script is invoked
>> directly, that is:
>> $ ./whatever.SlackBuild
>> or
>> $ sh -c ./whatever.SlackBuild
>>
>> otherwise, shebang is nothing more than a comment...
>>
>> If you run your script through sh (which links to bash), it doesn't
>> matter what the shebang is, it can be #!/bin/csh, and the script is
>> still going to be interpreted by sh.
>>
>> This might be important, as sbopkg uses sh to execute the build.
>>
>> --
>> Best regards,
>> Andrzej Telszewski
>
> Are you sure? I tried to test this using the following script
> (executable bit is set):
>

Yes, I'm sure;)

> -- start ./test_script
> #!/bin/bash
> printf '$0=%s\n' "$0"
> printf 'BASH_VERSION=%s' "$BASH_VERSION"
> -- end ./test_script
>
> When run by all the shells I have (ash,bash,dash,ksh,mksh,tcsh),
> they all output the same. I used the following loop to run
> the commands:
>
> for s in ash bash dash ksh mksh tcsh sh csh ; do
> /bin/$s -c ./test_script
> done

That's it.  If you use -c switch, then the script is executed as if you 
used ./, that is, the shebang is processed.  Try running your test 
without the -c switch and placing some constructs that are only 
understood by one of the shells.

$ sh -c ./whatever.SalckBuild
is equivalent to:
$ ./whatever.SalckBuild

but

$ sh ./whatever.SalckBuild
will process the script by whichever shell is linked to the sh, without 
taking into account the shebang (maybe there are shells, that upon 
seeing shebang in the first line don't treat it as comment, but I don't 
know about it).

>
> The output for each is identical:
> $0=./test_script
> BASH_VERSION=4.3.39(1)-release
>
> But if I change the shebang line to '#!/bin/dash', then the
> output for all is:
> $0=./test_script
> BASH_VERSION=
>
> At the time of running this /bin/sh -> /bin/bash. Changing
> /bin/sh to point anything else (other than tcsh/csh, I never
> set /bin/sh to a c shell) did not change the results.
>
> I'm sure I could carry this testing further, but I think
> the point might be proved well enough. I didn't want to
> step into this discussion, as I have rather conservative
> opinion about proper use of shebang and would rather not
> go down this road here. But let's keep things accurate,
> as far as how things actually work.
>
> John
> _______________________________________________
> SlackBuilds-users mailing list
> SlackBuilds-users at slackbuilds.org
> http://lists.slackbuilds.org/mailman/listinfo/slackbuilds-users
> Archives - http://lists.slackbuilds.org/pipermail/slackbuilds-users/
> FAQ - http://slackbuilds.org/faq/
>
>


-- 
Best regards,
Andrzej Telszewski


More information about the SlackBuilds-users mailing list