bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Variable scoping


From: Pierre Gaston
Subject: Re: Variable scoping
Date: Fri, 25 Jul 2008 08:58:21 +0300

On Fri, Jul 25, 2008 at 2:07 AM, Dan Stromberg
<dstromberglists@gmail.com> wrote:
>
> Having a shell function's variable changes reflected in its caller really
> kinda makes me shudder - in fact, it reminds me of gosub.  It seems like
> a bug waiting to happen; I'm amazed I haven't been bitten by it yet.
>
> It occurred to me that this might help - but of course it's probably
> quite a bit slower:
>
>        #!/bin/sh
>
>        function fn
>        {(
>                variable=2
>        )}
>
>        variable=1
>        fn
>        echo $variable


This script is wrong, you should not use the form "function fn" with
sh, it is not defined by posix   (you also don't need  the { } )
fn ()
(
 variable =2
)

> Is there a better way today?

With  "sh" (a posix shell) no
In bash you can use the builtin "local" (or declare).




reply via email to

[Prev in Thread] Current Thread [Next in Thread]