User Tools

Site Tools


scripting:bash:returnvar

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

scripting:bash:returnvar [2015/01/28 12:13] – created warnaudscripting:bash:returnvar [2020/12/17 05:23] (current) – external edit 127.0.0.1
Line 1: Line 1:
 +When you want to save values between functions of your bash script, there are several ways:
  
 +  - Use the same variable name and don't create them as "local" to a function <code bash>function myfunc()
 +{
 +    myresult='some value'
 +}
 +
 +myfunc
 +echo $myresult</code> :!: Quite Dangerous :!:
 +  - Let you function handle arguments: <code bash>function myfunc()
 +{
 +    local  __resultvar=$1
 +    local  myresult='some value'
 +    eval $__resultvar="'$myresult'"
 +}
 +
 +myfunc result
 +echo $result</code>\\
 +
 +Reference: [[http://www.linuxjournal.com/content/return-values-bash-functions]]