0

I'm trying to write a script to recursively find & replace a string in a directory. But I wanted to exclude certain directories such as .git/ from it.

In assembling a command string, the following command works if used directly (to replace "F" with "R":

find . -not \( -path '.git' -prune \) -type f -print0 | xargs -0 sed -i 's/\"F\"/\"R\"/g'

But if I place the same thing in a string and then invoke it:

CMD="find . -not \( -path '.git' -prune \) -type f -print0 | xargs -0 sed -i 's/\"F\"/\"R\"/g'"

$CMD

It fails with an error

find: paths must precede expression: \(

I've tried a few things with changing the quotes between single and double; and removing the \ in \(. None works so far.

Does anyone know what went wrong and how to make the quoted command work?

thor
  • 21,418
  • 31
  • 87
  • 173

1 Answers1

-1

You should try putting expression inside ``

Shiv Prakash
  • 33
  • 1
  • 6
  • That would string-split and glob-expand the output and execute it as a command itself. It doesn't fix the issue, but causes additional problems (or would, if there *were* any output rather than an error). – Charles Duffy Apr 07 '18 at 21:44