As the final edits revealed, the problem is unrelated to the dollar sign, but is caused by the content of deststr
, which is not 192.168.1.3 192.168.1.4
but rather two lines, one containing only 192.168.1.3
and the other containing only 192.168.1.4
, both lines bveing terminated with a newline character. That is, the actual command after variable replacement is:
sed "25s/Allow from .*/Allow from 192.168.1.3
192.168.1.4
/" -i test2
Now sed
interprets its command line by line, and thus the first command it tries to interpret is:
25s/Allow from .*/Allow from 192.168.1.3
which clearly is an unterminated s
command, and thus reported by sed
as such.
Now the solution you found, using echo
, works because
echo $var
calls echo with two arguments (because the whitespace is not quoted, it is interpreted as argument delimiter), the first one being 192.168.1.3
and the second one being 192.168.1.4
; both are forms that are not interpreted further by the shell.
Now echo
just outputs its (non-option) arguments separated by a space, therefore you now get as command line:
sed "25s/Allow from .*/Allow from 192.168.1.3 192.168.1.4/" -i test2
as intended.
Note however that for command substitution, instead of backticks you should use $()
whereever possible, since it's too easy to get backticks wrong. Therefore the follwing does what you want:
sed "$A s/Allow from .*/Allow from $(echo $destStr)/" -i test2
Note that I also took advantage of the fact that sed
allows a space between address and command, to simplify the quoting. In situations where such an extra space is not possible, you can also use the following syntax:
sed "${A}s/Allow from .*/Allow from $(echo $destStr)/" -i test2
Also note that this relies on the fact that the non-space characters in destStr
are interpreted neither by the shell, nor by sed
if occurring in the replacement string.
bash
. – celtschk Aug 07 '14 at 12:29bash
too. – Nidal Aug 07 '14 at 12:31destStr
have the literal valuestring
? – celtschk Aug 07 '14 at 12:33sed
withecho
(but leave the arguments unchanged)? – celtschk Aug 07 '14 at 12:45the value contains "\n"
. – Nidal Aug 07 '14 at 12:48destStr
with the command above, but got it from somewhere else, and whereever you got it from inserted newline characters in between. That's your real problem (it is completely unrelated to the dollar sign), and that's also why yourecho
solution works:echo
replaces that line feed with a simple space. – celtschk Aug 07 '14 at 12:52