For less fiddling with escaping special characters such as double quotes (and $
if you use it), you could just put the variable itself in double quotes and the rest in single quotes. No spaces between.
's|\("browser.startup.homepage",\) "\(.*\)"|\1 '"$ddrs"' |'
On a separate note, since you don't refer back to the second value, there's no reason to use parentheses for it:
's|\("browser.startup.homepage",\) ".*"|\1 '"$ddrs"' |'
Likewise, if you want your variable value $ddrs
to appear in double quotes in the file you're editing, you'll need to include those double quotes:
's|\("browser.startup.homepage",\) ".*"|\1 "'"$ddrs"'" |'
But then, it occurs to me, you probably have more than one value on a single line (or you could have.) Since sed regexes are greedy, your .*
will match the rest of the line up to the last double quote. To make it non-greedy (for this specific use case), use a character class matching anything but a double quote:
's|\("browser.startup.homepage",\) "[^"]*"|\1 "'"$ddrs"'" |'
And, although an accidental match would be unlikely, the dots (.
) in the first string aren't matching dots, they're matching the character class "any single character". To avoid that you escape them:
's|\("browser\.startup\.homepage",\) "[^"]*"|\1 "'"$ddrs"'" |'
It looks like you're using a comma delimiter, but then you match one space ONLY after the comma—not two spaces or zero. I suspect that any number of spaces there is possible, so for robustness, allow arbitrary spaces between the comma and the double quote:
's|\("browser\.startup\.homepage",\) *"[^"]*"|\1 "'"$ddrs"'" |'
There are no other obvious issues here. (1) The trailing space I will assume is deliberate; (2) I suppose |
is a fairly safe character to assume won't be in URL (the contents of $ddrs
). The escaping here is a bit of a nightmare to read, but for this specific use specifically in sed
I think that's unavoidable.
Robustness is more important than readability, though readability is good to strive for where it doesn't sacrifice robustness.
$ddrs
should not be expanded, so what is there to "bypass"?ddrs=$(shuf -n1 urls.txt)