Don't escape the backslashes; you'll confuse yourself. Use a different symbol after the s
command that doesn't appear in the text (I'm using %
in the example below):
line_old='myparam /path/to/a argB=/path/to/B xo'
line_new='myparam /path/to/c argB=/path/to/D xo'
sed -i "s%$line_old%$line_new%g" /etc/myconfig
Also, enclose the whole string in double quotes; using single quotes means that sed
sees $line
(in the original) instead of the expanded value. Inside single quotes, there is no expansion and there are no metacharacters. If your text can contain almost any plain text character, use a control character (e.g. control-A or control-G) as the delimiter.
Note that the use of -i
here mirrors what is in the question, but that assumes the use of GNU sed
. BSD sed
(found on Mac OS X too) requires a suffix. You can use sed -i '' …
to replace in situ; that does not work with GNU sed
. To be portable between the two, use -i.bak
; that will work with both — but gives you a backup file that you'll probably want to delete. Other Unix platforms (e.g. AIX, HP-UX, Solaris) may have variants of sed
that do not support -i
at all. It is not required by the POSIX specification for sed
.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…