3 ms·
> In summary, what you propose here is not only vanishingly atypical, but also bad coding practice. What I'm describing has been SOP in bash for twenty years.
by Spivak 7y ago
> In summary, what you propose here is not only vanishingly atypical, but also bad coding practice.
What I'm describing has been SOP in bash for twenty years. You can program in a more modern style and replace some uses of eval with ${!indirect_references}, declare, and associative arrays but eval is still in wide use today. Search through /etc for eval with grep and you'll return plenty of results in the wild.
For example the eval in my previous post could be rewritten as the following
declare -g "$1"="result"
but this form, although safer, is the less common usage.
Assigning to global variables or passing in a variable name to get the return value is just how bash works. See $MAPFILE $OPTIND $OPTARG for examples of the former and read as an example of the latter.
- LukeShu 7y agoThat [declare -g] is bad too, because it means you can't use it to set a local variable (see my sibling comment about dynamic scoping), which is surprising and confusing to the caller. While I agree with the parent that the whole thing is disgusting, I also recognize that it's a valid optimization technique, and it's sometimes necessary. So, if you do go down that route, I'd encourage you to do it as printf -v "$1" '%s' "$result"