Issue
I was using grep in the linux terminal yesterday to find instances of Perl code where the 'grep' function is used. I forgot to add quotes around the word 'grep', and noticed something semi-interesting. For example:
git grep grep
... instead of:
git grep 'grep'
... would cause the terminal to hang. To satisfy my own curiosity, I was trying to research exactly what is happening behind the scenes when this command is executed, with no luck.
Can someone please explain to me why exactly this command causes the terminal to hang, but git grep
by itself does not, an instead throws a fatal error for 'no pattern given'?
Solution
There's no chance quoting that arg in any standard shell changes anything. If the two commands behaved differently you were facing hardware (or, if your disk or system is remote, network) difficulties
Shells use your command line to construct args to the execve
system call, in the simplest cases, like yours, the first word of the command is used to hunt down the program to execute and every string on the command line is passed as arguments. Quoting in the command line is used to shut off some of the shell's preprocessing, those quotes aren't passed to the invoked command -- and no preprocessing is needed for a one-word arg. So git grep grep
and git grep 'grep'
produce identical git
invocations.
In a lot of cases the quotes are there for human benefit not for any practical effect. Newbies get confused by the presence or absence of quotes in shell code sometimes because they expect them to have more significance than they do, they forget that the shell's main job is to call execve
with a bunch of strings and the standard files all pointed to the right places.
git init --template=''
is the accepted way to init a no-template minimal repo, but if you remember me saying those quotes aren't passed to the started command, you'll understand that the above is identical to
git init --template=
and if your sense of humor's working you'll enjoy
git init '--'template=
which is also identical, because the quotes just shut off a lot of preprocessing that doesn't affect that quoted text anyway. If your shell's bash
say man bash
and hunt up shell expansions.
Answered By - jthill Answer Checked By - Terry (WPSolving Volunteer)