One of the ways replacing a string with sed
can be done as follows:
sed -i 's/old_str/new_str/' file.txt
However if replace.sh
is
sed -i 's/$1/$2/' $3
the command ./replace.sh old_str new_str file.txt
doesn't seem to work -- after making replace.sh
executable of course. Why is that?
I am aware rpl
does exactly the same as I intend to do with replace.sh
, but I'd like to understand why is it not working.
'
character prevents$1
from being expanded and you have the literal string$1
. If you use"
" instead then it'll work... but beware of magic characters in a solution like this;./replace.sh my/string new/stuff myfile
won't work!