Question

I try to use a variable in a sed of bash

I have this code :

read -p "Repertoire destination: " REP
echo $REP

sed -i 's/\$app\[\"bundle.root\"\] = \"\/var\/www\/\"\;/\$app\[\"bundle.root\"] = \"'$REP'\"\;/'  /home/martialp/Documents/default.php

echo "Modification terminé"

I use simple quote like '$REP' but i have this error :

sed: -e expression n°1, caractère 80: option inconnue pour `s'
Was it helpful?

Solution

Your trouble is probably that $REP contains slashes, but you're using / to delimit the regular expression. The easiest fix is to use some other character to delimit the regular expression, perhaps %:

sed -i 's%\$app\[\"bundle.root\"\] = \"\/var\/www\/\"\;%\$app\[\"bundle.root\"] = \"'$REP'\"\;%'  /home/martialp/Documents/default.php

You can use any character that doesn't otherwise appear in the command; Control-A, for example, works well and is unlikely to appear in $REP.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top