3

Hopefully I am not the only one that finds globs to misbehavior. If you'd asked me an hour ago, I would have been 99% confident that

test/src/**/*.js

would match all .js files in src and all subdirectories of src..but I am on MacOS and using Bash 3.2.57, and it's not matching more than one file.

Anyone know what might be wrong?

2
  • 2
    Your shell might be wrong. First enter zsh and then do your globbing. SCNR
    – Philippos
    Commented Jul 18, 2017 at 7:44
  • Haha :) i am stuck on bash for now Commented Jul 18, 2017 at 15:52

2 Answers 2

5

Bash 3.2 doesn't support globstar:

$ shopt -s globstar
bash: shopt: globstar: invalid shell option name

Without shopt -s globstar, ** is just a plain non-recursive glob.

The bash NEWS page says:

This is a terse description of the new features added to bash-4.0 since the release of bash-3.2. As always, the manual page (doc/bash.1) is the place to look for complete descriptions.

  1. New Features in Bash

    [...]

    w. There is a new shell option: globstar. When enabled, the globbing code treats ** specially -- it matches all directories (and files within them, when appropriate) recursively.

You'll need to update your bash to at least 4.0 (probably install one from homebrew), or use find.

2
  • is the shopt builtin specific to bash only, but the set builtin is part of posix, i.e. all "bourne" shells have it? Commented Jul 18, 2017 at 6:58
  • I don't know about other shells, but yes, shopt is not POSIX and set is.
    – muru
    Commented Jul 18, 2017 at 7:15
1

You could use find to do this instead. For example:

find ./test/src -type f -name \*.js

If you want to limit to only 2 directories deep:

find ./test/src -maxdepth 2 -type f -name \*.js

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .