Shell wildcards and dot files
By default, bash (the shell) doesn't expand a filename beginning with a dot (considered a hidden file) unless the dot is explicitly specified. Since [.]
is a pattern, rather than a literal character, it doesn't trigger the display of hidden files and so ls -d [.][!.]?*
doesn't match anything.
You can change this default behavior by running shopt -s dotglob
, which sets the option to always includes hidden files when expanding filename patterns (known as "globbing").
Example (the dollar sign at the beginning of the line indicates the shell prompt; don't type it in):
$ touch .foo
$ ls -d .[!.]?*
.foo
$ ls -d [.][!.]?*
ls: cannot access [.][!.]?*: No such file or directory
$ shopt -s dotglob
$ ls -d [.][!.]?*
.foo
The issue with echo [D]
is that expansion doesn't happen if no filename is matched. Since you didn't have a file named D
when you tried it, echo
was given, and echoed, what you literally entered.
An example of pathname expansion only occurring when a filename matches:
$ touch apple
$ echo a*
apple
$ rm apple
$ echo a*
a*
If you delete all of the files which match [D]*
, then echo [D]*
will give you [D]*
.
Related videos on Youtube
naxchange
Updated on September 18, 2022Comments
-
naxchange almost 2 years
Why do the following two codes work as expected:
ls -d .[!.]?* echo [D]*
But the following 2 don't:
ls -d [.][!.]?* echo [D]
In the first command, I get an error:
ls: cannot access [.][!.]*: No such file or directory
When I am trying to get the same listing
ls -d .[!.]?*
. And for the second, the output is:[D]
When I'm expecting an error proclamation along the lines of
No such file or directory
. What am I missing? What exactly makes an expression a wildcard, if those wildcard elements shown in the second set of examples above don't cut it?Clarification (Also in comments):
The wildcard
[D]*
doesn't only outputD
, it also outputsDesktop
,Downloads
... etc. However, I also triedecho [D]
when I had a file named D and when I didn't. The output worked when the fileD
was there, but I also got the output[D]
when it wasn't. I don't get why. Why did the presence of the file in the directory change the expression [D] from being a wild card to not?-
Admin about 11 yearsYou have a file named
D
in that directory? -
Admin about 11 yearsYes, for practice :D
-
Admin about 11 yearsBut not when you got the output that you put in your question. You shouldn't say, "When I'm expecting the file named
D
," when no file by the name exists at the time. -
Admin about 11 yearsFirst of all, I really appreciate that you took the time to provide help that you gave, and I apologize for the poor wording of my original question. But, what I actually implied with the "I am expecting the file named
D
" was an error proclamation along the lines ofNo such file or directory
.
-
-
naxchange about 11 yearsOk, that clears things up. What about the second example? Any ideas?
-
Gilles 'SO- stop being evil' about 11 years@naxchange Are you absolutely sure that you have a file called
D
? Ifecho [D]
prints[D]
butecho [D]*
seems to printD
, that indicates that you have a file whose name isD
and one or more whitespace or unprintable character. -
naxchange about 11 yearsThe wildcard
[D]*
doesn't only outputD
, it also outputsDesktop
,Downloads
... etc. However, I also triedecho [D]
when I had a file namedD
and when I didn't. The output worked when D was there, but I also got the output[D]
when it wasn't. I don't get why. Why did the presence of the file in the directory change the expression[D]
from being a wild card to not? -
depquid about 11 years@naxchange You've changed from what you put in your question. Please don't do that in the comments; edit your question if necessary. Anyway, I've updated my answer accordingly.