| Subject: | Re: Variable scoping |
|---|---|
| From: | "Pierre Gaston" <pierre.gaston@xxxxxxxxx> |
| Date: | Fri, 25 Jul 2008 08:58:21 +0300 |
| Newsgroups: | gnu.bash.bug |
On Fri, Jul 25, 2008 at 2:07 AM, Dan Stromberg
<dstromberglists@xxxxxxxxx> 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).
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| ||
| Previous by Date: | Re: patch number 035, Chet Ramey |
|---|---|
| Next by Date: | Re: Variable scoping, Chet Ramey |
| Previous by Thread: | cannot pipe output of trap, proway . imbusch |
| Next by Thread: | Re: Variable scoping, Stephane Chazelas |
| Indexes: | [Date] [Thread] [Top] [All Lists] |