The below script works, but what purpose does the declare $line
have? If I remove it, it doesn't work.
And what is the difference of {} \;
and {} +
in the find
command?
awk '{print "old="$1" new="$2}' list.txt |\
while IFS= read line; do
declare $line
find /path -name '*.ext' -exec sed -i "s/\b$old\b/$new/" {} +
done
The declare or typeset Builtins, which are exact synonyms, permit modifying the properties of variables. This is a very weak form of the typing available in certain programming languages. The declare command is specific to version 2 or later of Bash. The typeset command also works in Ksh scripts.
The
declare
is setting variables: Yourawk
command emits contents of the formold=foo new=bar
. Runningdeclare 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):To make this a bit safer, one can also escape literal content against being treated as regular expressions:
Note that I haven't changed the use of
\b
, an extension which many implementations ofsed
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: