User Tools

Site Tools


scripting:bash:returnvar

When you want to save values between functions of your bash script, there are several ways:

  1. Use the same variable name and don't create them as “local” to a function
    function myfunc()
    {
        myresult='some value'
    }
     
    myfunc
    echo $myresult

    :!: Quite Dangerous :!:

  2. Let you function handle arguments:
    function myfunc()
    {
        local  __resultvar=$1
        local  myresult='some value'
        eval $__resultvar="'$myresult'"
    }
     
    myfunc result
    echo $result


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

scripting/bash/returnvar.txt · Last modified: 2015/01/28 12:13 by warnaud