Issue
I'm trying to run a grep search from my PHP application and am having trouble with one of the options. The grep command is as follows:
grep -rn --exclude-dir={img,node_modules,vendor} -E "14847"
This command returns the expected results. However, if I open PHP interactively and run this line:
print `grep -rn --exclude-dir={img,node_modules,vendor} -E "14847"`;
I get results found within the node_modules
directory. Interestingly, if I remove the curly braces and commas and only exclude node_modules
, then it works and excludes those results. I can also just list the --exclude-dir
option separately for each directory to exclude (but let's ignore that for a moment). So it seems to be something with the handling of curly braces or commas. I have tried escaping both with backslashes with no luck unfortunately. Am I missing something in PHP's parsing of the execution operator?
Solution
The {one,two,three}
notation is a bash-ism, it's not expanded by either PHP or grep. When you run a command in backticks from within PHP, it runs that program via sh, not bash. Thus, you lose the possibility to use this type of expansion. As a workaround, you can just specify --exclude-dir
more than once:
print `grep -rn --exclude-dir=img --exlude-dir=node_module --exclude-dir=vendor -E "14847"`;
Answered By - Alex Howansky Answer Checked By - Mildred Charles (WPSolving Admin)