Re: Is there a POSIX compliant way of turning a "HH:MM:SS" formatted string to seconds? ...
first: thank you for your valuable input!
On Friday, July 18, 2025 1:30:27 PM CEST, Greg Wooledge wrote:
On Fri, Jul 18, 2025 at 12:34:36 +0200, Michael wrote:
On Friday, July 18, 2025 12:52:03 AM CEST, Greg Wooledge wrote: ...
Bash 4.3 and higher have nameref variables, [...]
although i was aware of nameref variables in bash, i never used them. i
wonder, why... well, maybe in the future i might change to using them,
instead of printf -v...
The advantage of nameref variables is that they can also refer to
arrays as well as strings. If you need to return an array, this is
the way you'd want to go (unless you're targeting older systems).
my oldest system is running debian 9 with bash version 4.4... so i should
be clear in this regard... (the now ancient debian version is another topic
we won't discuss here... ;) )
Both ways work, with one important caveat. If the function doesn't use
any local variables at all, then these approaches work perfectly well.
However, things start to become ugly if the function uses local variables
and the caller passes a variable name that collides with one of them.
that is true! but hey, it's bash... maybe in some version of the future
they come up with namespaces for functions or something equivalent. but i
guess that would break a lot of scripts out there...
There is unfortunately *no* way at all to define a local reference to
a variable in a different scope. You're at the mercy of bash's
dynamic scoping rules.
i'm fully aware...
The only workaround for that is to make all of your local variables
obscure, so that the caller is unlikely to duplicate them. Prefixing
every local variable with something like _myfunc_ (underscore,
function name, underscore) is one way to ensure a lack of collisions,
as long as the caller doesn't go out of their way to cause breakage.
Obviously, this makes the code uglier.
you're right, i agree...
but, on a completely different topic: since you suggested a prefix
'_myfunc_'...
iirc, i think i have read in your wiki to NOT have variable names with
leading underscores...
greetings...
Reply to: