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:
Why might bash be unhappy with this statement?
case $prev in
# other cases omitted
-@(F|-file|-targets))
_filedir
return 0;
;;
# other cases omitted
esac
$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.
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.
$1 - The first argument sent to the script. $2 - The second argument sent to the script.
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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With