Enabling ** in your bash

Updated . Posted . Visible to the public.

You may know the double asterisk operator from Ruby snippets like Dir['spec/**/*_spec.rb'] where it expands to an arbitrary number of directories.
However, it is disabled by default on most systems. Here is how to enable it.

If you check your globstar shell option, it is probably disabled:

$ shopt globstar
globstar           off

In that case, ** behaves just like * and will match exactly 1 directory level.

$ ls spec/**/*_spec.rb
spec/models/user_spec.rb

To enable it, run

shopt -s globstar

The double asterisk will then expand to any number of directories:

$ ls spec/**/*_spec.rb
spec/world_spec.rb
spec/models/user_spec.rb
spec/models/user/authentication_spec.rb

To persist this setting, you need to run it everytime you open your shell, i.e. put it into your .bashrc (on Ubuntu there is even a commented-out line that you can just put back in).

If you changed only your bashrc, re-open your shell (or source ~/.bashrc).

Arne Hartherz
Last edit
Arne Hartherz
License
Source code in this card is licensed under the MIT License.
Posted by Arne Hartherz to makandra dev (2017-01-10 10:40)