Sed string substitution with Shell Variable

Input:

$ cat testing.list
deb [trusted=yes] http://10.47.4.220/repos/test-repo/11  /

$ echo $REPOVER
12

Expected output:

$ cat testing.list
deb [trusted=yes] http://10.47.4.220/repos/test-repo/12  /

Please note the change in the number at the end.

Attempt 1 with single quotes (Not working):

$ sed -r 's#(.*/)([[:digit:]]+)([[:space:]]+/)$#\1$REPOVER\3#' testing.list
deb [trusted=yes] http://10.47.4.220/repos/test-repo/$REPOVER  /

Attempt 1.1 with single quotes without the variable (Working):

 $ sed -r 's#(.*/)([[:digit:]]+)([[:space:]]+/)$#\112\3#' testing.list
deb [trusted=yes] http://10.47.4.220/repos/test-repo/12  /

Attempt 2 with double quotes (Not Working):

$ sed -r "s#(.*/)([[:digit:]]+)([[:space:]]+/)$#\1$REPOVER\3#" testing.list
sed: -e expression #1, char 44: unterminated `s' command

Attempt 2.1 with double quotes without the variable (Not Working):

$ sed -r "s#(.*/)([[:digit:]]+)([[:space:]]+/)$#\112\3#" testing.list
sed: -e expression #1, char 44: unterminated `s' command

So, it seems like sed does not like double quotes. However, Shell won't expand variables unless I use double quotes.

FWIW:

$ bash --version | head -1
GNU bash, version 5.0.3(1)-release (x86_64-pc-linux-gnu)

$ sed --version
sed (GNU sed) 4.7
Packaged by Debian
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.


Read more here: https://stackoverflow.com/questions/68470187/sed-string-substitution-with-shell-variable

Content Attribution

This content was originally published by slayedbylucifer at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: