You type the command at the shell prompt. The shell processes what you typed, which includes globbing, substituting variables, substituting $()
and so on. After processing what you typed, the shell executes the command.
Quotes are needed if a string contains characters that are special to the shell, such as spaces or asterisks, but you don't want the shell to process them. You will get away without quoting an asterisk if there are no matching files in the current directory, but it's good practice to quote it anyway.
It is important to understand that the --regex
option has no effect on the shell's actions. First, the shell processes the command that you type. locate
gets the result of that processing.
a) If there are files in the current directory that match file*
, the shell will replace file*
with the list of those files before calling locate
. If there is no match, the shell won't touch file*
, and locate looks for files that are named file
, filee
, fileee
and so on.
In short, the shell attempts globbing, then locate
performs a regular expression search if the shell's globbing results in correct syntax.
b) The quotes tell the shell to leave the asterisk alone. locate
will look for files that start with file
. No regex search.
c) The shell attempts globbing as in a). If there is no match, locate
will look for files that start with file
. No regex search.
d) The shell leaves the expression alone. locate
will perform a regex search and look for files that are named file
, filee
, fileee
and so on.
failglob
andnullglob
as options), whereas zsh would only pass the unmodified pattern tolocate
with optionnonomatch
. See for example Why zsh tries to expand * and bash does not? – steeldriver Jan 28 '21 at 15:47locate '*file*'
, then locate will search for all file paths in Db having keywordfile
in it. So, is not it again a regex search ? If yes, then is not--regex
redundant ? – Number945 Jan 28 '21 at 16:19locate '*file*'
, here locate is using to search through database and not shell expanding the wildcard. So, what is this process called ? Still we call it globbing or regex ? – Number945 Jan 28 '21 at 17:31locate '*file*'
is not a regex search. It finds files that contain the string file (actually, the wildcards are redundant here).locate --regex '*file*'
is regular expression search. It finds filenames that start with an asterisk, then fil, then any number of e's. – berndbausch Jan 28 '21 at 22:35locate 's*p'
findssleep
,smtp
among others. The asterisk stands for "any string". On the other hand, inlocate --regex 's*p'
, the asterisk means "a sequence of zero or more of the previous character", i.e. the empty string, a single s, or any number of s's. – berndbausch Jan 29 '21 at 00:00