Can I set up my own sub-environment?

Paul Smith paul at mad-scientist.net
Tue Feb 8 21:38:33 UTC 2011


On Tue, 2011-02-08 at 18:31 +0000, David Collier wrote:
> OK - I always thought that there was a fundamental incompatibility
> between the syntax for bash functions and that for ash
> But I couldn't recall the details when challenged above.
> 
> so bash has
>     function fred{}
>  or
>     fred(){}
>     
> and ash has
>     fred{}
>     
> which means I was right originally - bash and ash scripts containing
> functions are syntactically incompatible.
> 
> yes ?????

No.

As Denys points out (and others including me have pointed out before)
your syntax for function declarations in ash is wrong.

Any POSIX-based shell, and that includes bash, ash, dash, ksh, zsh, and
all the others others, supports function definitions using this syntax:

        <funcname> () { <funcbody>; }

(where the final semicolon can be omitted if there's a newline before
the closing brace).  That syntax is required by POSIX and is 100%
portable across any Bourne-like shell you'll ever find in use today.

Bash, as Denys has pointed out, adds lots and lots of new features in
addition to the features required by POSIX.  Some of these are actually
extremely useful and make bash scripts more powerful than POSIX sh
scripts. Others, not so much.

One of the features bash adds is the extra (note EXTRA, as in "in
addition to", not "instead of") ability to define a function using a
syntax of:

        function <funcname> { <funcbody>; }

Again as Denys has pointed out, this is an utterly useless bit of
language over-specification and you should simply pretend it never
existed, and never use it.  Define your functions using the POSIX form
and it will work everywhere.

Sven had an excellent suggestion: write your scripts so they work with
ash.  99.9% of the time they'll then be portable to any POSIX-based
shell, including bash.



More information about the busybox mailing list