0

I am trying to update several config files with web addresses

the config files are a generic file that all contain a line such as

"github" => "github_change",

my sed command is

sudo sed -i 's/github_change/"$github"/g' config.php

but this just produces

"github" => ""$github"",

proper output should be

"github" => "https://github.com",

Ive tried for awhile now on different quote combinations to no end

1 Answers1

0

Single quotes prevent variable expansion. You are probably looking for something more like

sed -i "s/github_change/$github/g" config.php

Since your address has forward slashes, you will need to escape those:

sed -i "s/github_change/${github//\//\\/}/g" config.php

About the weird construct: normally ${varname//a/b} expands to the contents of varname, but replacing all occurrences of a with b. In this case a is really /, but it has to be escaped to be recognized as a literal forward slash instead of the separator between a and b, so \/. The last part is \\/, which is a literal backslash (escaped) followed by a forward slash.

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
  • thank you for the answer. So then I am assuming this would only work if its a straight forward domain? If you had something like https://somewebsite/index.php?topic=2509979 the above wouldnt work? – Jonathan Adams Dec 14 '17 at 21:03
  • 1
    I don't think you'd have to escape the question mark, it has no special meaning to sed. – Benjamin W. Dec 14 '17 at 21:07
  • Actually, it'll be fine as-is for that example. Only ampersand and slash mean anything in the replacement: https://stackoverflow.com/q/407523/2988730 – Mad Physicist Dec 14 '17 at 21:10
  • BTW, consider selecting the answer in preference to saying "thank you". You're quite welcome though. – Mad Physicist Dec 14 '17 at 21:12