I have a nest of bash scripts that can source each other, e.g. a.sh sources b.sh sources a.sh etc... Other than redesigning, which may not always be feasible, what are the best practices for avoiding a source loop?

One approach I have is to use the presence of a given function that indicates, not to source this scrip again. e.g.

    declare -F a
    if [ $? -eq 0 ]
    function a()

another is to have some per file variable "$sourced_a_sh" and if set return.

They all seem pretty kludgy however. Is there a nicer way?


You can add this as first line in your script a.sh:

[ "$sourced_a_sh" != "" ] && return || sourced_a_sh=.
  • Nice. It's certainly concise 8) – Neil McGill Aug 26 '14 at 18:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.