Is the behaviour of .* to include . and .. defined in LSB or POSIX or some other specification?
Quoting from the Single Unix specification version 2, volume ”Commands & Utilities", §2.13.3:
If a filename begins with a period (
.
) the period must be explicitly matched by using a period as the first character of the pattern or immediately following a slash character. (…) It is unspecified whether an explicit period in a bracket expression matching list, such as[.abc]
can match a leading period in a filename.
There is no exception that would make the second period in ..
, or the empty string following the only period in .
, not matched by the wildcard in .*
. Therefore the standard says that .*
matches .
and ..
, annoying though it may be.
The passage above describes the behavior of the shell (sh
command). The section on the glob
C library function refererences this passage.
The language is exactly the same in version 3, also known as POSIX:2001 and IEEE 1003.1-2001, which is what most current systems implement.
Dash, bash and ksh93 comply with POSIX. Pdksh and zsh (even under emulate sh
) don't.
In ksh, you can make .*
skip .
and ..
by setting FIGNORE='.?(.)'
, but this has the side effect of making *
include dot files. Or you can set FIGNORE='.*'
, but then .*
doesn't match anything.
In bash, you can make .*
skip .
and ..
by setting GLOBIGNORE='.:..'
, but this has the side effect of making *
include dot files. Or you can set GLOBIGNORE='.*'
, but then .*
doesn't match anything.
Probably you mean the functionality in bash expansion about globignore. By default the bash expansion match . and .. but reading the man:
The GLOBIGNORE shell variable may be used to restrict the set of file names matching
a pattern. If GLOBIGNORE is set, each matching file name that also matches one of
the patterns in GLOBIGNORE is removed from the list of matches. The file names ``.''
and ``..'' are always ignored when GLOBIGNORE is set and not null. However, setting
GLOBIGNORE to a non-null value has the effect of enabling the dotglob shell option,
so all other file names beginning with a ``.'' will match. To get the old behavior
of ignoring file names beginning with a ``.'', make ``.*'' one of the patterns in
GLOBIGNORE. The dotglob option is disabled when GLOBIGNORE is unset.
You can set the variable GLOBIGNORE=.:..
so when you tipe something like this:
rm -r * .*
you are removing only the current directory. The POSIX standard only specify that . is the current directory and .. in the parent of the current directory. The special meaning of .* is interpreted by bash or other shells (or programs like grep).