shell--函数返回值

时间:2014-06-02 17:19:17   收藏:0   阅读:190
bubuko.com,布布扣
#!/bin/bash

function myfun()
{
    echo "echo result"
    return 0
}


returnValue=$(myfun)

echo "${returnValue}"
bubuko.com,布布扣

这里returnValue得到的并不是0,而是"echo result",想要得到function内的return 值, 要用$?

 

输出:

$ bash -x test.sh
++ myfun
++ echo ‘echo result‘
++ return 0
+ returnValue=‘echo result‘
+ echo ‘echo result‘
echo result

 

 

得到返回值,应该像下面这么写

bubuko.com,布布扣
#!/bin/bash

function myfun()
{
    echo "echo result"
    return 0
}


returnValue=$?

echo "${returnValue}"
bubuko.com,布布扣

输出

$ bash -x test.sh
+ returnValue=0
+ echo 0
0

shell--函数返回值,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!