Faculty of Engineering and Mathematical Sciences 
Not logged in (login)


This forum is provided to promote discussion amongst students enrolled in Open Source Tools and Scripting (CITS4407).
RSS cloud
Jump to:

Returning Multiple Values: string manipulation vs global variables vs alternatives.

1 of 390 articles shown, currently no other people reading this forum.
From: Lee dB.
Date: Mon 25th May 2020, 12:09pm


Hi everyone,

I'm looking at how to better structure the shell scripts for my project, and one thing I've 
found challenging is how to best pass multiple values between functions.

One way I've used is to concatenate the multiple values together, echo them to stdout, then 
deconstruct them back into variables in the receiving function. This seems expensive, 
especially since this might require multiple forks. It's also not great for code reuse as 
it's hard to put a function together that can do this multiple times (because how would a 
helper function to deconstruct a string for passing multiple parameters itself return 
multiple parameters to the calling function?).

So that leaves one of the other options being global variables, which are traditionally 
considered to Be a Bad Thing. But I'm not sure what other alternatives there are whilst 
still making good use of functions for code modularity.

Other languages that don't allow multiple return values usually at least allow returning a 
pointer, or having parameters passed in by reference, so there are other ways of tackling 

Is there another mechanism I've missed for returning multiple values from functions that's 
easy on the eyes, maintainable and efficient? What are the prevailing thoughts on global 
variables in shell scripting achieving this, versus string processing?


Related articles

Returning Multiple Values: string manipulation vs global variables vs alternatives. (all 6) RSS
├─ THIS   Mon 25th May 2020, 12:09pm, Lee dB.
├─ reply 1   Tue 26th May 2020, 7:53am, Christopher M.
├─ reply 2   Tue 26th May 2020, 1:49pm, Lee dB.  O.P.
├─ reply 3   Tue 26th May 2020, 5:44pm, Christopher M.
├─ reply 4   Mon 1st Jun 2020, 9:23am, Lee dB.  O.P.
└─ reply 5   Mon 1st Jun 2020, 11:15am, Christopher M.
This Page

Program written by: [email protected]
Feedback welcome
Last modified: 11:27am Sep 21 2020