Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

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?

share|improve this question
    
I was counting the number of parenthesis you have at the end statement. It may be possible you have too many. –  ryekayo Sep 16 '14 at 17:25
    
No, I checked that multiple times. I think you found 1 extra paren which is a part of the switch-case syntax. This function currently works for me on Arch Linux, too. –  darnir Sep 16 '14 at 17:26
1  
have you enabled extglob before trying to define that function? –  llua Sep 16 '14 at 17:33
    
Damn! I'm enabling extglob, but after the function is defined. Once I fixed the order, it works! I never came across this issue in the past, maybe all the other systems were setting extglob globally. Thanks a bunch! –  darnir Sep 16 '14 at 17:38

1 Answer 1

up vote 6 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.