I’m trying to escape characters within backticks in my bash command, mainly to handle spaces in filenames which cause my command to fail.
The command I have so far is:
grep -Li badword `grep -lr goodword *`
This command should result in a list of files that do not contain the word “badword” but do contain “goodword”.
Your approach, even if you get the escaping right, will run into problems when the number of files output by the
goodwordgrepreaches the limits on command-line length. It is better to pipe the output of the firstgreponto a secondgrep, like thisThis will correctly handle files with spaces in them, but it will fail if a file name has a newline in it. At least GNU
grepandxargssupport separating the file names with NUL bytes, like thisEDIT: Added double dashes
--togrepinvocations to avoid the case when some file names start with-and would be interpreted bygrepas additional options.