You have two options.
1) The easiest solution is export newName
. If you don't export the variable, then it's not available in subshells, and bash -c
is a subshell. That's why you're getting TEMPLATE
replaced by nothing.
2) Alternatively, you can try to construct a correctly quoted command line containing the replacement of $newName
. If you knew that $newName
were reasonably well-behaved (no double quotes or dollar signs, for example), then it's easy:
find . -name '*TEMPLATE*' \
-exec bash -c 'mv "$0" "${0/TEMPLATE/'"${newName}"'}"' {} \;
(Note: bash quoting is full of subtleties. The following has been edited several times, but I think it is now correct.)
But since you can't count on that, probably, you need to construct the command line by substituting both the filename and the substitution as command line parameters. But before we do that, let's fix the $0
. You shouldn't be using $0
as a parameter. The correct syntax is:
bash -c '...$1...$1...' bash "argument"
Note the extra bash
(many people prefer to use _
); it's there to provide a sensible name for the subprocess.
So with that in mind:
find . -name '*TEMPLATE*' \
-exec bash -c 'mv "$1" "${1/TEMPLATE/$2}"' bash {} "$newName" \;