Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash case syntax - meaning of "-@"

Tags:

I'm not familiar with the semantics of the "-@" in the bash script snippet below, which is from /etc/bash_completion.d/subversion. I'm trying to understand why bash reports "syntax error near unexpected token '(' on this line, I have two questions:

  1. What is "-@()" expected to do here?
  2. Why might bash be unhappy with this statement?

    case $prev in
                    # other cases omitted
        -@(F|-file|-targets))
            _filedir
            return 0;
            ;;
                    # other cases omitted
            esac
    
like image 963
Lance Richardson Avatar asked Jul 14 '09 12:07

Lance Richardson


People also ask

What does [- Z $1 mean in bash?

$1 means an input argument and -z means non-defined or empty. You're testing whether an input argument to the script was defined when running the script. Follow this answer to receive notifications.

What is mean by $? In shell script?

The $? variable holds the exit status of a command, a function, or of the script itself. $$ process ID variable. The $$ variable holds the process ID [4] of the script in which it appears.

What is $1 and $2 in bash?

$1 - The first argument sent to the script. $2 - The second argument sent to the script.

What is Echo $$ in bash?

The echo command is used to display a line of text that is passed in as an argument. This is a bash command that is mostly used in shell scripts to output status to the screen or to a file.


2 Answers

The '@(...)' here is part of Bash's pattern matching syntax. It's an "or", it simply matches one of the listed patterns, separated by pipe characters.

The initial dash has simply been factored out of the expression; "-@(a|b|c)" is the same as "@(-a|-b|-c)", but shorter.

As pointed out by a commenter (thanks!) this requires Bash's extglob enabled to work. This is done like so:

shopt -s extglob

You can check if you already have it enabled like this:

shopt extglob
like image 161
unwind Avatar answered Sep 28 '22 14:09

unwind


I've found the answer to the second part of my question, which is that in order for the "@(...)" to be enabled, the "extglob" shell option needs to be on. Executing:

shopt -s extglob

before executing the script eliminates the error.

like image 34
Lance Richardson Avatar answered Sep 28 '22 14:09

Lance Richardson