How do I use variables and non-variables in a single sed command?
You can go two different ways:
-
Using single quotes: break out of the single quotes and reference the variable (using double quotes to prevent word splitting and filename expansion):
sudo sed -i 's/$sUrl . $this_sOutDir/https:\/\/'"$bucketname"'.s3.amazonaws.com . $this->_sOutDir/g' /var/www/html/$name/core/oxconfig.php
-
Using double quotes: escape the
$
characters to be treated literally:sudo sed -i "s/\$sUrl . \$this_sOutDir/https:\/\/$bucketname.s3.amazonaws.com . \$this->_sOutDir/g' /var/www/html/$name/core/oxconfig.php
However, the caveat is: if $bucketname
contains sed
special characters / sequences, the sed
command will break (in this case since the variable is to be expanded in the replacement section, the concern is about it possibly containing the separator, accidental backreferences or &
.
One way to go around this is sanitazing the variable beforehand by escaping possible separators, accidental backreferences and &
characters to prevent them from being interpreted as such.
Another way is using Perl instead of sed
, passing the variable directly to Perl which will handle special characters at replacement time ($
characters to be treated literally must be escaped even if using single quotes with this method):
sudo perl -i -spe 's/\$sUrl . \$this_sOutDir/https:\/\/$bucketname.s3.amazonaws.com . \$this->_sOutDir/g' -- -bucketname="$bucketname"
$ echo '$sUrl . $this_sOutDir' | perl -spe 's/\$sUrl . \$this_sOutDir/https:\/\/$bucketname.s3.amazonaws.com . \$this->_sOutDir/g' -- -bucketname="string/with/separators/&/and/accidental\$1backreferences"
https://string/with/separators/&/and/accidental$1backreferences.s3.amazonaws.com . $this->_sOutDir
Related videos on Youtube
Peter Franke
Updated on September 18, 2022Comments
-
Peter Franke over 1 year
I'm trying to use
sed
to replace some PHP lines.My problem is I want to replace a line with PHP variables with a string including a Bash variable.
Here is an example:
sudo sed -i 's/$sUrl . $this_sOutDir/https:\/\/'$bucketname'.s3.amazonaws.com . $this->_sOutDir/g' /var/www/html/$name/core/oxconfig.php
As you can see, I want to replace
$sUrl . $this->_sOutDir
(literally) with the s3 URL, which includes a Bash variable. I'm trying to use double quotes but it's not working for me:sudo sed -i 's/$sUrl . $this_sOutDir/https:\/\/"$bucketname".s3.amazonaws.com . $this->_sOutDir/g' /var/www/html/$name/core/oxconfig.php
I'm also trying to use the complete
s/...
in double quotes but it's not working becausesed
thinks that the first variables are also Bash variables.The result is:
return https://.s3.amazonaws.com . $this->_sOutDir . '/';