Bash: Syntax Error on @()

bashwildcards

I have the following function in my .bashrc file:

extract() {
    local c e i

    (($#)) || return

    for i; do
        c=''
        e=1

        if [[ ! -r $i ]]; then
            echo "$0: file is unreadable: \`$i'" >&2
            continue
        fi

        case $i in
        *.t@(gz|lz|xz|b@(2|z?(2))|a@(z|r?(.@(Z|bz?(2)|gz|lzma|xz)))))
               c='bsdtar xvf';;
        *.7z)  c='7z x';;
        *.Z)   c='uncompress';;
        *.bz2) c='bunzip2';;
        *.exe) c='cabextract';;
        *.gz)  c='gunzip';;
        *.rar) c='unrar x';;
        *.xz)  c='unxz';;
        *.zip) c='unzip';;
        *)     echo "$0: unrecognized file extension: \`$i'" >&2
               continue;;
        esac

        command $c "$i"
        e=$?
    done

    return $e
}

now this has been working perfectly for me on my current Arch Linux system. Recently, I installed the new, void-linux distro and tried to use my old .bashrc on it.

However, on Void-Linux, this functions throws an error:

syntax error near unexpected token '('

and points to this line:

*.t@(gz|lz|xz|b@(2|z?(2))|a@(z|r?(.@(Z|bz?(2)|gz|lzma|xz)))))

Some investigation returned that the Bash on that distro refuses to read the @() pattern and hence returns an error. I remember using the same function on a Debian Stable system a couple of months ago too.

Can anyone point out why this code doesn't seem to be portable? And where the error is?

Best Answer

As mentioned in my comment, that is a common error people see when trying to use extended globs(shopt -s extglob) before enabling the option. This includes function definitions.

Related Question