help-bash
[Top][All Lists]
Advanced

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

[Help-bash] How to unset a local variable to expose the corresponding gl


From: Peng Yu
Subject: [Help-bash] How to unset a local variable to expose the corresponding global variable in the function?
Date: Sat, 6 Aug 2016 21:05:12 -0500

Hi, The following example shows that once the variable tmp is set
local in a function my_fun, the function can not access the
corresponding global variable. Is there a way to still get the global
variable as if the local variable has not been set at all?

~$ cat ./main2.sh
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

set -v
tmp=10
my_fun() {
    local tmp=x
    unset tmp
    echo "$tmp"
}

my_fun
echo "$tmp"

~$ ./main2.sh
tmp=10
my_fun() {
    local tmp=x
    unset tmp
    echo "$tmp"
}

my_fun

echo "$tmp"
10


-- 
Regards,
Peng



reply via email to

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