With recent bash you can use namerefs and pass your function the names of the variables to assign: my_function value err
. Example:
$ my_function() {
local -n v="$1" e="$2"
v="foo bar"
e="baz qux"
}
$ my_function value err
$ echo "$value"
foo bar
$ echo "$err"
baz qux
You can also design your function to print two string values on the standard output separated by a custom separator that cannot be found in the string values (for instance a newline) and assign this to an array:
$ my_function() {
local v e
v="foo bar"
e="baz qux"
printf '%s\n%s' "$v" "$e"
}
$ IFS=$'\n' arr=($(my_function))
$ echo "${arr[0]}"
foo bar
$ echo "${arr[1]}"
baz qux
And again with namerefs you can have kind of named aliases for the array elements:
$ declare -n value=arr[0] err=arr[1]
$ echo "$value"
foo bar
$ echo "$err"
baz qux