2

Wildcards

An asterisk (*) – matches one or more occurrences of any character, including no character.

Can anyone explain what is this “ including no character” means?

1 Answers1

5

Bash's manual phrases it a bit differently (just to pick some source):

* Matches any string, including the null string

The "null string" is the zero-length string, so the meaning is that * matches any number of any characters, even none at all. Or, zero or more of any character.

foo*bar matches e.g.

  • foo1bar (where the * matches one character)
  • foo22bar (two chars)
  • but also just foobar, where the * matches the zero-length string between foo and bar.

etc.

(Also, *.txt would also match the filename .txt, but filenames starting with dots are a special case and don't get matched unless the dot is explicitly given in the pattern, or if something like Bash's dotglob is set.)

If you want to require at least one character, you can use ?* since ? requires and matches exactly one character.

ilkkachu
  • 138,973
  • I’m just beginning to study Linux ,Ihow can it mache 0 matches?cant make sense of it. – user599592 Jan 20 '24 at 11:44
  • 1
    @user599592, "how" in what sense? I don't understand what you mean. It's just defined and implemented so that it doesn't require there to be any characters there. – ilkkachu Jan 20 '24 at 12:04
  • @user599592 As ilkkachu showed, the pattern foo*bar matches the string foobar, which means that the * inside that pattern is allowed to match nothing. – Kusalananda Jan 20 '24 at 12:20