Can I keep . and .. out of .* expansion?

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 .*?


Solution 1:

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.

Solution 2:

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.