How do I use variables and non-variables in a single sed command?

5,686

You can go two different ways:

  1. 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
    
  2. 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
Share:
5,686

Related videos on Youtube

Peter Franke
Author by

Peter Franke

Updated on September 18, 2022

Comments

  • Peter Franke
    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 because sed thinks that the first variables are also Bash variables.

    The result is:

    return https://.s3.amazonaws.com . $this->_sOutDir . '/';