I would like to include files into a shell script as "support" scripts. Since they set things up and check the environment, I only want to let them be executed once.
My approach was to create a global array, check if the script name exists in that array. f it doesn't, add it to the array and "source" the external file.
The current code looks like this:
 function array_contains() { # array, value
    local element
    for element in $1; do
        [[ "$element" == "$2" ]] && return 0
    done
    return 1
}
declare -a LOADED_SUPPORT_SCRIPTS
function require_support() { # file
    # Check if we loaded the support already
    array_contains ${LOADED_SUPPORT_SCRIPTS[@]} $1 && return 0
    # Add loaded support to array
    LOADED_SUPPORT_SCRIPTS=("${LOADED_SUPPORT_SCRIPTS[@]}" "$1")
    log -i "Including support for '$1'"
    source "$BASE_DIR/supports/$1.sh"
    return 1
}
While this should work in theory (at least for me), this code just fails every single time. Somehow the array gets reset (even though I never access it somewhere else) and always contains a "/" entry in the beginning.
From further googling my problem I found this "solution" which needs a new variable name inside every script that I want to include - like C/C++ does. While this is indeed a good idea, i would like to keep my code as small as possible, I do not really care about the performance of array iterations.
I really would like to know if there is an alternative way, or what I did wrong in my code that could be fixed. Thanks in advance!
 
     
     
     
     
    