I´m building a bash script and i want to replace “${PRODUCT_NAME}” with a “WORD” for example.
The problem is “$”, in bash $ stands for an argument so when i run the script
echo "Replace ${PRODUCT_NAME} with $1 in document.txt"
for file in $(grep -il ${PRODUCT_NAME} ~/Documents/tmp/document.txt)
do
sed -e "s/${PRODUCT_NAME}/$3/g" $file > /tmp/tempfile.tmp
mv /tmp/tempfile.tmp $file
done
the script recognizes the ${PRODUCT_NAME} as an argument and not as a word that i want to replace. How do i make it recognize that “${PRODUCT_NAME}” is a word and not an argument, so i can replace it?
First of all, note that your
sedscript actually needs to use\${PRODUCT_NAME}rather than${PRODUCT_NAME}as the search-pattern, because in a POSIX Basic Regular Expression,$means “end of line” rather than “dollar sign”. That out of the way . . .In Bash, you can either quote a dollar-sign with a backslash:
or else use single-quotes (which are like double-quotes except that they don’t allow
$-based expansion, and don’t handle\):(By the way, is it intentional that your
echoline uses$1while yoursedline uses$3? It seems like they should both use the same parameter.)