这闻起来很臭,但可能有人可以解释一下:
以下脚本不起作用,输出如下:
#!/bin/bash
GLOBAL_VAR="OLD"
myfunc() {
echo "func before set> $GLOBAL_VAR"
GLOBAL_VAR="NEW"
echo "func after set> $GLOBAL_VAR"
}
myfunc | cat
echo "final value> $GLOBAL_VAR"
输出:
func before set> OLD
func after set> NEW
final value> OLD
现在,只需取下| cat
它,它就可以工作了!
#!/bin/bash
GLOBAL_VAR="OLD"
myfunc() {
echo "func before set> $GLOBAL_VAR"
GLOBAL_VAR="NEW"
echo "func after set> $GLOBAL_VAR"
}
myfunc
echo "final value> $GLOBAL_VAR"
输出:
func before set> OLD
func after set> NEW
final value> NEW