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?