Having $string2 defined as the complete output makes no sense (to me) as you could simply print it without any substitution. I guess this is more appropriate:
$> string1='encoding=""'; string2=UTF-8; echo $string1| sed 's/""/'$string2'/g'
encoding=UTF-8
sed is a StreamEditor and so needs a stream as input to work on. Having no file or redirected input won't make it work.
#!/bin/sh
string1="encoding="""
string2="encoding="UTF-8"
sed 's/'"$string1"'/'"$string2"'/g'
Needs to be:
#!/bin/sh
string1="encoding=\"\""
string2="encoding=\"UTF-8\""
sed 's/$string1/$string2/g'
Another thing I don't see here is what stream are you passing to sed?
You have no file name at the end of the sed command and you are not piping anything into the front of it, so sed has no stream to work on.
To edit a text file, a text editor seems a natural choice.
With ex:
ex -sc '%s/encoding=""/encoding="UTF-8"/g|x' file
With ed:
ed -s file <<'EOED'
1,$s/encoding=""/encoding="UTF-8"/g
w
q
EOED
A less readable but equivalent ed one-liner:
printf '%s\n' '1,$s/encoding=""/encoding="UTF-8"/g' w q | ed -s file
On an unrelated and pedantic note, neither GNU's sed -i ... nor perl -i edit anything in place. While those options are convenient, they create and rename a temp file just as you would by using shell redirection and the mv command. To the underlying filesystem, the result is a new file with a different inode. ed and ex, however, actually do edit the file in place.
in connection with this thread. i would like to ask if it is possible that I use sed in replacing a string in an entire column? how do i incorporate a specific column, like $2 to the format of sed since it only accepts string