zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    特殊集合
    推箱子
    集合
    数组

    循环语句 练习题
    穷举与迭代
    循环语句
    练习题
    switch case
  • 原文地址:https://www.cnblogs.com/helloweworld/p/3762810.html
Copyright © 2011-2022 走看看