10

I often find it annoying (or worse) when I type

command .*

in bash and the command applies to the parent directory and current directory. Is there some shell option or other configuration I can tweak to make bash exclude . and .. from the expansion of .*?

13

In bash, in this order:

GLOBIGNORE=.
shopt -u dotglob

When GLOBIGNORE is set, . and .. are automatically ignored, so you dont actually need to set them in GLOBIGNORE, but if you have nothing else to ignore you wont have anything to set it to.

Every time you set GLOBIGNORE to a non-null value, bash turns on the option dotglob, so you will need to turn it off afterwards; otherwise bash will expand * to include files starting with a dot.

  • Cool, thanks! I actually would rather have dotglob turned on, so setting GLOBIGNORE sounds perfect. – David Z Aug 21 '10 at 22:04
  • When GLOBIGNORE is set, Bash does ignore . and .. but not foo/. and foo/... You may want to use GLOBIGNORE=.:*/.:*/.. to make this work with path names too. – user123444555621 Aug 4 '14 at 9:17
3

On an unfamiliar system where I have too little time for checking the settings in use (like GLOBIGNORE) I use the following foolproof pattern:

command .??*
  • Quick to type and guaranteed to exclude . and .. regardless of the environment.
  • 1
    I always liked this approach, but be aware that it fails to match 1-char files .a – MestreLion Mar 28 '13 at 7:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.