0

In Bash's Manual, for filename expansion:

When a pattern is used for filename expansion, the character . at the start of a filename or immediately following a slash must be matched explicitly, unless the shell option dotglob is set.

When matching a filename, the slash character must always be matched explicitly.

In other cases, the . character is not treated specially.

Can you give some examples to explain the cases in the quote?

How shall I distinguish the usage of . in filename expansion from its unrelated usage of hardlink .?

3
  • 1
    The information you cite is basically explaining how filenames starting with . (e. g. .ssh or .bashrc) are hidden from display by default, and accordingly not matched in globs by default (i. e. ls * does not show dotfiles, including . and ...). . is not expanded, as it is not a glob, it is an explicit reference to the hardlink to the current directory. It happens to also start with ., and so is not included by default in globs such as *.
    – DopeGhoti
    Commented Dec 28, 2015 at 21:00
  • Thanks. What does "unless the shell option dotglob is set" mean? What do the last two sentences in the quote mean?
    – Tim
    Commented Dec 28, 2015 at 21:05
  • 1
    The shell option dotglob allows for hidden files to be matched with a glob such as *. The sentence about slashes means that by default you will not descend into subdirectories, which are denoted by /. The last sentence means that a . in the middle of a filename (e. g. filename.txt) is to be treated just like any other character (which is why filename.txt in the current directory will appear in the output of ls *).
    – DopeGhoti
    Commented Dec 28, 2015 at 21:08

1 Answer 1

5

There is no usage of . in filename expansion. If you use filename expansion, filenames which start with . are usually excluded, unless you explicitly asked for those, using something like .* or .?. / is not a valid character in a filename, so * doesn't match /.

$ touch foo .foo; echo *; echo .*; echo ./*; echo; ./.*
foo
.foo
./foo
./.foo

Note how * and ./* excluded .foo - yet the first echo command had no usage of ., and the third echo command used ./, so the wildcard is separated from the . by a /. To match filenames starting with ., you must explicitly specify the leading . - .*, ./.*. However, in this case the role of . is no different from, say, the role of a in a*.

This exception can be disabled by using shopt -s dotglob.

Since there is no special usage of . in filename expansion, there is nothing to distinguish. The only special characters in filename expansion are *, ?, [...].

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .