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 23 hours agobash
too. – Networker 23 hours agodestStr
have the literal valuestring
? – celtschk 23 hours agosed
withecho
(but leave the arguments unchanged)? – celtschk 22 hours ago