When writing bash scripts, I like to write stand-alone functions that take an argument and perform operations based on this / these arguments, rather than declaring global variables in several different places in the code that reduces readability.
The problem arises when you have a function that needs to use multiple variables. Passing some function, like 10 variables, is just ugly, and a simple associative array can be used for this.
If we want to declare these variables in an external file, the "source" command allows you to import them all.
Then the question arises, how can I list the variables declared ONLY inside this file so that I can build an associative array with them? I was able to use the "compgen" combination and the loop to create associative arrays from the variable list, but how to list only the variables found inside the file, regardless of what they are called, so I can loop them and build my array?
bash shell associative-array compgen
Byteflinger
source share