How to Handle Multiple Sourcing Issues with Readonly Variables in Bash?

0
7
Asked By CuriousCat42 On

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

Answered By TechieTom On

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.

SkepticalSam -

So, you mean to check if the variable is already declared and, if it is, just exit the function?

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.