Ell
Ell

Reputation: 947

What is the meaning of @(${VAR})?

Whilst looking at some shell scripts I encountered several instances of if statements comparing some normal variable against another variable which is enclosed in @( ) brackets.

Does @(....) have some special meaning or am I missing something obvious here? Example of if test:

if [[ ${VAR} != @(${VAR2}) ]]

Thanks

Upvotes: 3

Views: 161

Answers (2)

chepner
chepner

Reputation: 530882

It's an extended pattern, borrowed from ksh. Originally you would need to enable support for it with shopt -s extglob, but it became the default behavior inside [[ ... ]] in bash 4.1. @(...) matches one of the enclosed patterns. By itself, @(pattern) and pattern would be equivalent, so I would assume that the contents of $VAR2 contains at least one pipe, so that the expansion is something like @(foo|bar). In that case, the test would succeed if $VAR1 does not match foo or bar.

Upvotes: 9

Barmar
Barmar

Reputation: 780723

From the bash man page:

         @(pattern-list)
                 Matches one of the given patterns

So ${VAR2} is expected to be a list of patterns separated by |, and your code tests whether ${VAR} matches any of them.

Upvotes: 2

Related Questions