Is the behaviour of .* to include . and .. defined in LSB or POSIX or some other specification?

Is the behavior of .* to include . and .. defined in LSB or POSIX or some other specification?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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.

Method 2

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).

Method 3

The Linux man-page references POSIX.2, 3.13.

Method 4

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).


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x