Linux下Shell函数返回值实现种类

shell在执行的时候是顺序执行的,也不存在什么多线程什么的。

一下是实现种类:

1、全局

g_result=""  
  
function testFunc()  
{  
    g_result='local value'  
}  
  
testFunc  
echo $g_result  

2、局部

function testFunc()  
{  
    local_result='local value'  
    echo $local_result  
}  
  
result=$(testFunc)  
echo $result  

3、return返回特殊用法

function test() {
  return 1
}

echo $?

可以看出$?是用来接收的

参考:

https://www.linuxjournal.com/content/return-values-bash-functions

http://www.blogjava.net/xzclog/archive/2015/09/21/427407.html

https://blog.csdn.net/mdx20072419/article/details/9381339

猜你喜欢

转载自www.cnblogs.com/EasonJim/p/9616257.html