Bash – SED on Bash Script : how to replace text with special character that come from variable on Bash script

bashsed

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

Best Answer

First you need to quote your var1 variable.

You can use bash -c in your find command in order to give the password as a variable:

 var1='var\&2018+\&'
 find . -name "list.txt" -exec bash -c 'sed -i "s/1234/$2/g" $1' _ {} "$var1" \;