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, 17 Apr 2015 15:09:58 -0400
User-agent: Mutt/1.4.2.3i

On Fri, Apr 17, 2015 at 11:58:15AM -0700, Linda Walsh wrote:
> >outerfunc() {
> > func() { ...; }
> > func args
> > unset -f func
> >}
> >outerfunc

The problem is, ALL function definitions (and un-definitions) are global.
If there was a "func" defined at the global scope, you've obliterated it.
I don't understand why you don't understand this.

imadev:~$ f() { echo "global f"; }
imadev:~$ f2() { f() { echo "local f"; }; unset -f f; }
imadev:~$ f2
imadev:~$ f
bash: f: command not found

Bash is just like C in many ways.  This is one of them -- there is only
a single global namespace for all functions.

Doing trickery to make it LOOK LIKE you have a local function just leads
to confusion.  The person who inherits this script from you after you're
gone may think the function is local, when it isn't.

So, what is the point of doing this convoluted trickery, when it
accomplishes nothing (it doesn't create a local function, and it doesn't
prevent contamination of the global function namespace)?  Just to
see how "clever" you can be?



reply via email to

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