How to source a function file in Bash

Reusable Bash functions are easier to maintain when they live in a separate file instead of being copied into every script. Sourcing that file loads the definitions into the current shell process, so the caller can use the functions as if they were written in the same script.

The source builtin, and the equivalent . form, read shell code in the current context. A sourced function file needs read permission, not execute permission. Source only project files you trust, because variables, functions, shell options, and directory changes made inside the file affect the caller after the source command finishes.

The example keeps deploy-label formatting in project-functions.sh and imports it from deploy-check.sh. Passing an argument to source lets the function file initialize a profile value before the caller runs the loaded function.

Steps to source a Bash function file:

  1. Create the function file.
    project-functions.sh
    PROJECT_PROFILE=${1:-default}
     
    deploy_label() {
        local service=$1
        printf "%s-%s\n" "$PROJECT_PROFILE" "$service"
    }

    The function file is intended to be sourced, so it does not need a shebang or execute permission.

  2. Create a caller script that sources the function file.
    deploy-check.sh
    #!/usr/bin/env bash
    set -euo pipefail
     
    source ./project-functions.sh payment
     
    deploy_label api
    deploy_label worker

    The ./ prefix points to the function file in the current directory instead of relying on a name search. Keep that path explicit in scripts so the imported functions come from the intended project file.

  3. Check the function file syntax.
    $ bash -n project-functions.sh

    No output means Bash did not find a parse error.

  4. Check the caller script syntax.
    $ bash -n deploy-check.sh

    No output means Bash parsed the caller script without finding a syntax error.

  5. Run the caller script and confirm that the sourced function produced both labels.
    $ bash deploy-check.sh
    payment-api
    payment-worker
  6. Source the function file in the current shell.
    $ source ./project-functions.sh payment

    The payment argument becomes $1 while the file is being sourced, so project-functions.sh sets PROJECT_PROFILE for that shell.

  7. Verify that Bash registered the imported function.
    $ declare -F deploy_label
    deploy_label
  8. Run the imported function from the current shell.
    $ deploy_label api
    payment-api