Bash:@() 上的语法错误

dar*_*nir 2 bash wildcards

我的.bashrc文件中有以下功能:

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
}
Run Code Online (Sandbox Code Playgroud)

现在这在我当前的 Arch Linux 系统上对我来说非常有效。最近,我安装了新的 void-linux 发行版并尝试在其上使用旧的 .bashrc。

但是,在 Void-Linux 上,此函数会引发错误:

syntax error near unexpected token '('
Run Code Online (Sandbox Code Playgroud)

并指向这一行:

*.t@(gz|lz|xz|b@(2|z?(2))|a@(z|r?(.@(Z|bz?(2)|gz|lzma|xz)))))
Run Code Online (Sandbox Code Playgroud)

一些调查显示该发行版上的 Bash 拒绝读取 @() 模式,因此返回错误。我记得几个月前也在 Debian Stable 系统上使用了相同的功能。

谁能指出为什么这段代码似乎不可移植?错误在哪里?

llu*_*lua 6

正如我在评论中提到的,这是人们shopt -s extglob在启用该选项之前尝试使用扩展 globs( )时看到的常见错误。这包括函数定义。