Is the behavior of .* to include . and .. defined in LSB or POSIX or some other specification?
- 56,709
- 26
- 150
- 232
- 27,439
-
for how to glob hidden files except current and parent directory see: https://unix.stackexchange.com/questions/1168/how-to-glob-every-hidden-file-except-current-and-parent-directory – Lesmana Jun 05 '21 at 19:03
4 Answers
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.
- 829,060
-
note:
bashdoesn't always comply with POSIX. only when it's invoked assh. – strugee Nov 26 '13 at 19:46
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).
- 742
As far as I can tell, the LSB 4.1 does not require bash and only sh.
For sh it follows POSIX (with one minor non-relevant extension).
- 18,092
- 4
- 117
- 102
The Linux man-page references POSIX.2, 3.13.
- 57,532
-
Does the LSB require
man,man globor the man-pages to be present? I could not find man here (to my surprise). – Ciro Santilli OurBigBook.com Nov 26 '13 at 19:18