first, there is a variable that contain special character on bash script. here is the example. list.txt :
user = root
password = 1234
Bash Script :
#!/bin/sh
var1 = var&2018+&
find . -name "list.txt" -exec sed -i "s/1234/"$var1"/g" {} +
when i executed the script, the result is : var12342018+1234.
Update 1 (answer by Oliv) :
var1='var&2018+&'
find . -name "*list.txt" -exec bash -c 'sed -i "s/1234/$2/g" $1' _ {} "$var1" \;
and the result still : password = var12342018+1234
maybe anyone can help me. thank you
First you need to quote your
var1
variable.You can use
bash -c
in yourfind
command in order to give the password as a variable:it might help you understand the problem you are having to know that
&
is a special character that represent the whole match of your regexp.since
1234
is found, you are replacing it withvar12342018+1234
, the two&
is becoming the matched string.the other point I'd like to make is really a personal preference, but using
find ... -exec
with{}
, i don't like this syntax, I usually end it with\;
I prefer using xargs
find . -name list.txt | xargs sed -i 's/1234/var&2018+&/g'
note the ending g would replace it as many times as it is found...
also this might be obvious.. I don't know how many
list.txt
file you have in your subtree, but if you only have one, you don't needfind
.sed -i 's/pattern/replacement/options' filename