bug-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] make function local


From: Greg Wooledge
Subject: Re: [Help-bash] make function local
Date: Fri, 10 Apr 2015 08:07:36 -0400
User-agent: Mutt/1.4.2.3i

On Thu, Apr 09, 2015 at 05:40:30PM -0500, Peng Yu wrote:
> On Tue, Apr 7, 2015 at 8:49 PM, Chet Ramey <address@hidden> wrote:
> > Is there a particular problem you're trying to solve for which local
> > functions would be the appropriate solution?
> 
> One could also ask the same question for local variables. Any limited
> cases that show local variable is need, by definition, can be changed
> to ones in which global variables can also work.

Local variables in functions are extremely useful.  Moreover, they are
extremely frequently *used*, by real scripts.

Bash models the Bourne and Korn shells, which closely model the semantics
of C.  C has global functions with local variables.  So does bash.
If you want a private namespace for your functions, I'm afraid you're
out of luck.  You can prepend some fixed string to each function name
to sorta kinda emulate a namespace.

> However, the real problem is that global variable make larger programs
> difficult to maintain.

Correct!  You win a prize!(*)  Stop writing "larger programs" in bash.

> Therefore, the problem becomes severe for
> people using bash for making bash libraries.

Stop trying to write "bash libraries"!

http://mywiki.wooledge.org/BashWeaknesses

The sooner you come to grips with the fact that bash is a SHELL and not
a full-featured programming language, the happier you will be.


(*) Not really.



reply via email to

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