Search code examples
linuxbashwildcard

Why does bash documentation suggest quoting wildcard characters?


In several manuals (including the official GNU/Linux Command-Line Tools tldp.org) is recommended to use single (or double quotes) to avoid bash to interpret wildcards or regex symbols like caret (^) and dollar ($).

But in some examples that's not necessary to use single/double quotes. For instance:

(1)

$ touch 'fo*'

and

$ touch fo*

creates the same file fo* in both command line forms.

(2)

$ grep '^foo' file.txt

and

$ grep ^foo file.txt

both search for foo as a string in beginning of the lines of file.txt.

Why manuals recommend to use single/double quotes to avoid bash interpretation if bash in fact does not interpret some wildcards and regex symbols in first place?


Solution

  • If there is a file named foo in the current directory, then touch 'fo*' touches fo*, but touch fo* would expand to touch foo and touch that instead.

    If the nullglob option is set and no file name matching fo* exists, touch fo* would expand to just touch and complain about the missing operand.

    If the failglob option is set and no file name matching fo* exists, touch fo* would immediately cause an error.