I'm working on a project where I have a library directory named 'lib' that contains several shell scripts. In my 'out.sh' file, I define some functions for error handling, along with a few readonly variables. The issue arises because both 'main.sh' and 'arrays.sh' source 'out.sh', leading to errors when trying to modify the readonly variables. I want to keep the error handling function accessible from all library files but avoid modifying the readonly variables. What's the best way to manage this multiple sourcing without causing conflicts?
1 Answer
You could add a check like `[[ -v __STDOUT ]] && return` at the start of your scripts to prevent double sourcing. This way, if '__STDOUT' is already defined, it won't try to redefine it again. This should help you avoid the readonly variable errors.
So, you mean to check if the variable is already declared and, if it is, just exit the function?