Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why would I not leave extglob enabled in bash?

Tags:

bash

I just found out about the bash extglob shell option here:- How can I use inverse or negative wildcards when pattern matching in a unix/linux shell?

All the answers that used shopt -s extglob also mentioned shopt -u extglob to turn it off. Why would I want to turn something so useful off? Indeed why isn't it on by default? Presumably it has the potential for giving some nasty surprises. What are they?

like image 582
hardcode57 Avatar asked Jun 19 '13 12:06

hardcode57


People also ask

What is Extglob in Linux?

Enter extglob . As you can guess, it stands for extended globbing . This option allows for more advanced pattern matching. From man bash : extglob If set, the extended pattern matching features described above under Pathname Expansion are enabled.


2 Answers

No nasty surprises -- default-off behavior is only there for compatibility with traditional, standards-compliant pattern syntax.


Which is to say: It's possible (albeit unlikely) that someone writing fo+(o).* actually intended the + and the parenthesis to be treated as literal parts of the pattern matched by their code. For bash to interpret this expression in a different manner than what the POSIX sh specification calls for would be to break compatibility, which is right now done by default in very few cases (echo -e with xpg_echo unset being the only one that comes immediately to mind).

This is different from the usual case where bash extensions are extending behavior undefined by the POSIX standard -- cases where a baseline POSIX shell would typically throw an error, but bash instead offers some new and different explicitly documented behavior -- because the need to treat these characters as matching themselves is defined by POSIX.

To quote the relevant part of the specification, with emphasis added:

An ordinary character is a pattern that shall match itself. It can be any character in the supported character set except for NUL, those special shell characters in Quoting that require quoting, and the following three special pattern characters. Matching shall be based on the bit pattern used for encoding the character, not on the graphic representation of the character. If any character (ordinary, shell special, or pattern special) is quoted, that pattern shall match the character itself. The shell special characters always require quoting.

When unquoted and outside a bracket expression, the following three characters shall have special meaning in the specification of patterns:

  • ? - A question-mark is a pattern that shall match any character.
  • * - An asterisk is a pattern that shall match multiple characters, as described in Patterns Matching Multiple Characters.
  • [ - The open bracket shall introduce a pattern bracket expression.

Thus, the standard explicitly requires any non-NUL character other than ?, * or [ or those listed elsewhere as requiring quoting to match themselves. Bash's behavior of having extglob off by default allows it to conform with this standard in its default configuration.


However, for your own scripts and your own interactive shell, unless you're making a habit of running code written for POSIX sh with unusual patterns included, enabling extglob is typically worth doing.

like image 184
Charles Duffy Avatar answered Sep 21 '22 01:09

Charles Duffy


Being a Kornshell person, I have extglob on in my .bashrc by default because that's the way it is in Kornshell, and I use it a lot.

For example:

$ find !(target) -name "*.xml" 

In Kornshell, this is no problem. In BASH, I need to set extglob. I also set lithist and set -o vi. This allows me to use VI commands in using my shell history, and when I hit v, it shows my code as a bunch of lines.

Without lithist set:

for i in *;do;echo "I see $i";done 

With listhist set:

for i in * do     echo "I see $i" done 

Now, only if BASH had the print statement, I'd be all set.

like image 27
David W. Avatar answered Sep 20 '22 01:09

David W.