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: Peng Yu
Subject: Re: [Help-bash] make function local
Date: Thu, 9 Apr 2015 17:40:30 -0500

On Tue, Apr 7, 2015 at 8:49 PM, Chet Ramey <address@hidden> wrote:
> On 4/7/15 4:13 PM, Peng Yu wrote:
>> Hi, Currently, bash does not support local function. But it might be
>> helpful to add such a feature, so that function can become a first
>> class citizen in the language. Could this be considered in the future?
>
> 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.

Therefore, no matter why small examples that I should here, it will
always be criticized as can be solved by an alternative solution.

However, the real problem is that global variable make larger programs
difficult to maintain. Therefore, the problem becomes severe for
people using bash for making bash libraries. Any ad hoc use of bash
can always get away from this problem.

http://en.wikipedia.org/wiki/Global_variable

Does this make sense?

Regarding the usage of bash, I don't think it should be necessarily
limited to some restricted usage. It can be used as a full fledged
language, except that it does not handle many basic types as in C, but
it is far better in handle processes and files, and calling external
programs.. There are already some packages (like bash-completion)
developed. With better support from the language, it will make the
process of making packages easier.

If you must ask for an example, here is one. But again, people always
can find some walkaround (in this case rename the second g to g1, say)
for any code snippet as it is short by definition.

function g {
# g is defined in some bash library
# used by sourcing the library
}

function f {
# wants to define some local function so that it is only available in f
# accidentally g is choose
# Then call g in f
}

g #after a while g is called, but now g is the g in f but not the original g.

-- 
Regards,
Peng



reply via email to

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