Path: csiph.com!xmission!news.glorb.com!usenet.stanford.edu!not-for-mail From: Greg Wooledge Newsgroups: gnu.bash.bug Subject: Re: extglob syntax error in function definition Date: Thu, 8 Oct 2015 08:46:15 -0400 Lines: 26 Approved: bug-bash@gnu.org Message-ID: References: <20151006122732.3006A264E1A@mail.jonkmans.nl> <20151008034420.GA7819@ma.sdf.org> NNTP-Posting-Host: lists.gnu.org Mime-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit X-Trace: usenet.stanford.edu 1444308416 14406 208.118.235.17 (8 Oct 2015 12:46:56 GMT) X-Complaints-To: action@cs.stanford.edu To: bashbug@jonkmans.nl, bug-bash@gnu.org, bash@packages.debian.org Envelope-to: bug-bash@gnu.org Content-Disposition: inline In-Reply-To: <20151008034420.GA7819@ma.sdf.org> User-Agent: Mutt/1.4.2.3i X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 139.137.100.1 X-BeenThere: bug-bash@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Bug reports for the GNU Bourne Again SHell List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com gnu.bash.bug:11587 On Wed, Oct 07, 2015 at 10:44:20PM -0500, Eduardo A. Bustamante López wrote: > > Repeat-By: > > shopt -u extglob > > isnum () ( shopt -s extglob; case "$1" in [1-9]*([0-9])) return 0 ;; *) return 1 ;; esac; ) > > Remember that bash parses and interprets the script line-by-line. If you want > to change the parser's operation (for example, have it recognize the extglob > patterns), you have to do it in a different line than where you're using the > special syntax. Even more: bash parses an entire function all at once. If you want extglob syntax to be permitted inside a function, the extglob option must be turned on BEFORE the function is parsed. You can't flip it inside a function. The normal recommendation is that you should put shopt -s extglob right at the top of your script, directly beneath the shebang. #!/usr/local/bin/bash shopt -s extglob That way extglob is enabled for the entire script, functions and all. I'm not aware of any negative consequences for doing this. In fact, bash has a compile-time option to enable extglob. This isn't the default (yet), but perhaps some day it will be.