Wang Wang - 3 months ago 16
Bash Question

idioms for returning multiple values in shell scripting

Are there any idioms for returning multiple values from a bash function within a script? describes how to echo multiple values and process the results (e.g., example 35-17), but that gets tricky if some of the returned values are strings with spaces in.

A more structured way to return would be to assign to global variables, like

foo () {

echo "foo returned ${FOO_RV1} and ${FOO_RV2}"

I realize that if I need re-entrancy in a shell script I'm probably doing it wrong, but I still feel very uncomfortable throwing global variables around just to hold return values.

Is there a better way? I would prefer portability, but it's probably not a real limitation if I have to specify


Much as I love shell, it's probably the case that as soon as you're throwing arbitrary structured data around, Unix bourne/posix shell is not the right choice.

If there are characters which do not occur inside fields, then separate with one of those. The classic example is /etc/passwd, /etc/group and various other files which use a colon as a field separator.

If using a shell which can handle a NUL character inside strings, then joining on the NUL and separating on it (via $IFS or whatever) can work well. But several common shells, including bash, break on NUL. A test would be an old .sig of mine:

foo=$'a\0b'; [ ${#foo} -eq 3 ] && echo "$0 rocks"

Even if that would work for you, you've just reached one of the warning signs that it's time to switch to a more structured language (Python, Perl, Ruby, Lua, Javascript ... pick your preferred poison). Your code is likely to become hard to maintain; even if you can, there's a smaller pool of people who'll understand it well enough to maintain it.