In bash, how to store a return value in a variable?

前端 未结 7 2118
臣服心动
臣服心动 2020-11-29 09:06

I know some very basic commands in Linux and am trying to write some scripts. I have written a function which evaluates the sum of last 2-digits in a 5-digit number. The fun

相关标签:
7条回答
  • 2020-11-29 09:46

    Simplest answer:

    the return code from a function can be only a value in the range from 0 to 255 . To store this value in a variable you have to do like in this example:

    #!/bin/bash
    
    function returnfunction {
        # example value between 0-255 to be returned 
        return 23
    }
    
    # note that the value has to be stored immediately after the function call :
    returnfunction
    myreturnvalue=$?
    
    echo "myreturnvalue is "$myreturnvalue
    
    0 讨论(0)
提交回复
热议问题