POSIX-Compliant Way to Scope Variables to a Function in a Shell Script

前端 未结 7 634
被撕碎了的回忆
被撕碎了的回忆 2020-12-08 00:27

Is there a POSIX Compliant way to limit the scope of a variable to the function it is declared in? i.e.:

Testing()
{
    TEST=\"testing\"
}

Testing
echo \"T         


        
7条回答
  •  有刺的猬
    2020-12-08 01:14

    I believe the closest thing would be to put the function body inside a subshell.

    E.g. try this

    foo()
    {
      ( x=43 ; echo $x )
    }
    
    x=42
    echo $x
    foo
    echo $x
    

提交回复
热议问题