0

I have a bash script which have some helper function inside a shared, common file lib.sh. However, some of these functions look like this:

function check_prs {
    local labels
    labels=$(hub pr show -h "${BRANCH:-}" -F '%L')
    if [[ $PRLABELS == *"DOTHING"* ]]
    then true
    else false
    fi
}

The problem is that the command substitution and labels variable assignment is done when the script is loaded (source lib.sh from my main script), regardless of whether the function is called (or when).

Is there a way to only make these command substitutions when the function is actually called? Or some other way to make it behave as everything else in the normal flow of the script?

Christian P.
  • 4,784
  • 7
  • 53
  • 70
  • I can't reproduce this. Are you sure you're not calling the function somewhere? A minimal function that sets one variable and prints something does *not* set the variable and doesn't print anything when sourced. – Benjamin W. Aug 31 '20 at 12:34
  • What you describe is theoretically impossible. Nothing inside a function is executed until the function is called. You can add `set -x` at the beginning of your file to track down what is executed (and you may call `set +x` to stop displaying the details). – vdavid Sep 02 '20 at 15:01

0 Answers0