shell--函数返回值

#!/bin/bash

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


returnValue=$(myfun)

echo "${returnValue}"

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

 

输出:

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

 

 

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

#!/bin/bash

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


returnValue=$?

echo "${returnValue}"

输出

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

posted @ 2014-05-31 21:32  helloweworld  阅读(282)  评论(0编辑  收藏  举报