I can't explain the following behavior of unset
:
#!/bin/bash
my_unset() { unset "$@"; }
fn1() { local var; unset var; var=1; }
fn2() { local var; my_unset var; var=2; }
var=0
fn1; echo "$var"
fn2; echo "$var"
0
2
edit: I would expect the result to be 1
2
It behaves that way on bash 3/4/5
, is it a bug?
update
Here's an other example that shows that using my_unset
doesn't make the variable global; it only removes the local variable from the current context:
#!/bin/bash
my_unset() { unset "$@"; }
fn1() { local var; my_unset var; var=1; }
fn2() { local var; fn1; echo "[fn2] var=$var"; }
var=0
fn2
echo "[global] var=$var"
[fn2] var=1
[global] var=0