This is what I want to achieve:Function:Func1() {   $1=Hello World}Call function:local var1Func1 var1echo $var1  (should echo Hello World)I found this example which seems to work, but I guess using eval is not a good idea:Func1() {   eval $1=$str1}How would be the correct way of doing this?I'm coming from .Net and often use a parameter as a reference. For example, assigning a value back to the parameter which then can be used later on.In the above example var1 should be assigned Hello World (I'm using sh shell)
Save return value from a function in one of its own parameters
shell;variable;function
eval is fine if you use it properly:Func1() {  eval $1=\$str1}Is safe as long as Func1 is only called with the variable names you intend it to be passed.As always, you need to quote your variables. And here, $str1 doesn't need to be expanded before being passed to eval.If Func1 may be passed arbitrary strings that are not under your control, then that's where you'd get issues with values like reboot;foo and need to sanitize it like:Func1() {  case $1 in     |\    *[!abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_]* |\    [0123456789]* |\    BASH* | LD_* | PATH | IFS | ENV | SHELLOPTS | PERL5LIB | PYTHON_PATH |...)      echo >&2 Can't set that variable      return 1  esac  eval $1=\$str1}Coming up with a complete list of problematic variables is a doomed task though.