help-bash
[Top][All Lists]
Advanced

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

Re: [Help-bash] distinguish local function and global function


From: Nick Chambers
Subject: Re: [Help-bash] distinguish local function and global function
Date: Mon, 5 Mar 2018 11:21:29 -0600

Functions, much like variables are only always global. ie

NickChambers-iMac:~ Nick$ foo() {
> bar=2 # global bar
> }
NickChambers-iMac:~ Nick$ baz() {
> boo() { echo 'Hello, world!'; } # global boo
> }
NickChambers-iMac:~ Nick$ foo
NickChambers-iMac:~ Nick$ echo "$bar"
2
NickChambers-iMac:~ Nick$ baz
NickChambers-iMac:~ Nick$ boo
Hello, world!
NickChambers-iMac:~ Nick$

As far as I know, you can't have a local function like you can a
variable. They're only always global. You could create one in a
subshell, but I'm not sure if that's really what you're after.

On Mon, Mar 5, 2018 at 11:17 AM, Peng Yu <address@hidden> wrote:
> The following example shows that a local function can be defined
> within a function. But `type` (as the way that I use it) cannot
> distinguish them. Is there a way to tell what functions are local and
> what are global?
>
> $ cat main_x1.sh
> #!/usr/bin/env bash
> # vim: set noexpandtab tabstop=2:
>
> set -v
> function cmd {
> function cmd1 {
>     echo xxx
> }
> type cmd1
> cmd1
> }
>
> type cmd
> cmd
> $ ./main_x1.sh
> function cmd {
> function cmd1 {
>     echo xxx
> }
> type cmd1
> cmd1
> }
>
> type cmd
> cmd is a function
> cmd ()
> {
>     function cmd1 ()
>     {
>         echo xxx
>     };
>     type cmd1;
>     cmd1
> }
> cmd
> cmd1 is a function
> cmd1 ()
> {
>     echo xxx
> }
> xxx
>
>
> --
> Regards,
> Peng
>



reply via email to

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