The declare
is setting variables: Your awk
command emits contents of the form old=foo new=bar
. Running declare old=foo new=bar
sets those two variables.
That said, this is a wrong and sloppy way to do this. Instead, use read
to directly read the desired fields from the input file and assign to the variables (more on this in BashFAQ #1):
while read -u 3 -r old new _; do
find /path -name '*.ext' -exec sed -i "s/\b$old\b/$new/" {} +
done 3<list.txt
To make this a bit safer, one can also escape literal content against being treated as regular expressions:
requote() { sed 's/[^^]/[&]/g; s/\^/\\^/g' <<< "$1"; };
substquote() { sed 's/[&/\]/\\&/g' <<< "$1"; }
while read -u 3 -r old new _; do
find /path -name '*.ext' -exec \
sed -i "s/\b$(requote "$old")\b/$(substquote "$new")/" {} +
done 3<list.txt
Note that I haven't changed the use of \b
, an extension which many implementations of sed
won't support. See BashFAQ #21 for alternative approaches to doing literal string substitutions.
For completeness (though this unrelated topic really should have been asked as a separate question -- and could have, in that case, been closed as duplicate, as it's been asked and answered before), allow a quotation from the find
man page:
-exec command {} +
This variant of the -exec action runs the specified command on
the selected files, but the command line is built by appending
each selected file name at the end; the total number of invoca‐
tions of the command will be much less than the number of
matched files. The command line is built in much the same way
that xargs builds its command lines. Only one instance of `{}'
is allowed within the command. The command is executed in the
starting directory.