4

It seems to be a very useful feature. But I had to learn that it is often disabled by default. I though about enabling it on my system. But there is probably a good reason why it is disabled. Without knowning that, it seems unwise to enable it. Therefore my question: Why is the bash double star operator often disabled by default?

I found multiple questions about how it is enabled, what the operator is named and how to use it. All of them mention that it has to be enabled first. But none of the questions explains why it is disabled by default.

1 Answers1

6

Hard to say for sure without asking the developer.

In any case, it's an incompatible extension that was added to Bash only in 2009 (after being implemented earlier in zsh, ksh93 and yash). It's also one that can have surprising results if triggered accidentally. Perhaps even dangerous ones, e.g. if a script does something like

rm -- "$dir"/*$pattern*/*       # or
rm -- "$dir"/*"$string"*/*

without making sure $pattern is non-empty. Yes, both do trigger globstar in Bash (when enabled).

The same caveat applies to most other shells that have **: zsh, ksh93 -o globstar, tcsh (after set globstar) and yash -o extended-glob. Not fish, though: it has ** with somewhat different semantics, but it doesn't trigger it with something inbetween the two asterisks.

See e.g. Stéphane's answer in The result of ls * , ls ** and ls *** for the history and the differences between the implementations in various shells.

ilkkachu
  • 138,973
  • Might be worth pointing out that whilst zsh (where that feature comes from) had it almost from the start in 1992 (evolved from a previous design that used .... and **** in 1991), it was added to bash much later (in 2009) so with a much higher risk of breaking existing scripts would it have been enabled by default. – Stéphane Chazelas Jun 30 '22 at 08:11
  • The globstar name itself comes from ksh93 (which itself added the feature in 2003). The bash design of that feature is actually closer to that of ksh93's that that of zsh's. It's likely the bash developers just copied ksh93 including the fact that it was not enabled by default there. – Stéphane Chazelas Jun 30 '22 at 08:21
  • Note that whilst bash can be compiled with --enable-extended-glob-default, there's no equivalent --enable-globstar-default compile-time option so I doubt globstar is ever the default anywhere in bash. Recursive globbing with ** is only the default in zsh and fish AFAIK (and the fish one is significantly different from the zsh/yash or ksh93/bash designs) – Stéphane Chazelas Jun 30 '22 at 08:25
  • yeah, I was sloppy in putting Bash and zsh side-by-side there – ilkkachu Jun 30 '22 at 08:49