idioms for returning multiple values in shell scripting

Posted by Wang on Stack Overflow See other posts from Stack Overflow or by Wang
Published on 2010-03-21T21:06:27Z Indexed on 2010/03/21 21:11 UTC
Read the original article Hit count: 812

Filed under:
|
|

Are there any idioms for returning multiple values from a bash function within a script?

http://tldp.org/LDP/abs/html/assortedtips.html 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 () {
    FOO_RV1="bob"
    FOO_RV2="bill"
}

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 #!/bin/bash.

© Stack Overflow or respective owner

Related posts about shell

Related posts about shell-scripting