Skip to content

Fix array expansion with bash 3.x - #634

Merged
mbaldessari merged 1 commit into
validatedpatterns:mainfrom
mbaldessari:macosx-bash-3
Sep 9, 2026
Merged

Fix array expansion with bash 3.x#634
mbaldessari merged 1 commit into
validatedpatterns:mainfrom
mbaldessari:macosx-bash-3

Conversation

@mbaldessari

Copy link
Copy Markdown
Contributor

On macosx which has bash 3.x

    michele@Micheles-Mac-mini ~ % bash --version
    GNU bash, version 3.2.57(1)-release (arm64-apple-darwin25)

    michele@Micheles-Mac-mini ~ % cat /tmp/t.sh
    set -euo pipefail

    PKI_HOST_MOUNT_ARGS=()
    echo "FOO"
    echo "${PKI_HOST_MOUNT_ARGS[@]}"
    michele@Micheles-Mac-mini ~ % /tmp/t.sh
    FOO
    /tmp/t.sh: line 6: PKI_HOST_MOUNT_ARGS[@]: unbound variable

Where as the same just works on bash 4.x/5.x:

    bash --version
    GNU bash, version 5.3.9(1)-release (x86_64-redhat-linux-gnu)
    ❯ cat /tmp/t.sh
    set -euo pipefail

    PKI_HOST_MOUNT_ARGS=()
    echo "FOO"
    echo "${PKI_HOST_MOUNT_ARGS[@]}"
    ❯ /tmp/t.sh
    FOO
    ❯

We fix this by using the expression:

${VAR_ARRAY[@]+"${VAR_ARRAY[@]}"}

This works both on bash 3.x and also the newer ones. Tested on linux
with bash 5.x and on Mac OSX with bash 3.x

On macosx which has bash 3.x

        michele@Micheles-Mac-mini ~ % bash --version
        GNU bash, version 3.2.57(1)-release (arm64-apple-darwin25)

        michele@Micheles-Mac-mini ~ % cat /tmp/t.sh
        set -euo pipefail

        PKI_HOST_MOUNT_ARGS=()
        echo "FOO"
        echo "${PKI_HOST_MOUNT_ARGS[@]}"
        michele@Micheles-Mac-mini ~ % /tmp/t.sh
        FOO
        /tmp/t.sh: line 6: PKI_HOST_MOUNT_ARGS[@]: unbound variable

Where as the same just works on bash 4.x/5.x:

        bash --version
        GNU bash, version 5.3.9(1)-release (x86_64-redhat-linux-gnu)
        ❯ cat /tmp/t.sh
        set -euo pipefail

        PKI_HOST_MOUNT_ARGS=()
        echo "FOO"
        echo "${PKI_HOST_MOUNT_ARGS[@]}"
        ❯ /tmp/t.sh
        FOO
        ❯

We fix this by using the expression:

    ${VAR_ARRAY[@]+"${VAR_ARRAY[@]}"}

This works both on bash 3.x and also the newer ones. Tested on linux
with bash 5.x and on Mac OSX with bash 3.x
@mbaldessari

Copy link
Copy Markdown
Contributor Author

Once I soak this for a bit here in MCG I'll push to patternizer

@dminnear-rh dminnear-rh left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@mbaldessari
mbaldessari merged commit 9e4aefb into validatedpatterns:main Sep 9, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants