MSmth
MSmth

Reputation: 81

How can I use sed to replace a string with the content of a variable, when the variable has whitespace?

How can I use sed to replace a string with the content of a variable, when the variable has whitespace?

For example

sed 's_3_4 5_g'
234

returns 24 54, but

replace="4 5";  sed 's_3_'$replace'_g'
234

gives me the error

sed: 1: "s_3_4": unterminated substitute in regular expression

Upvotes: 0

Views: 1137

Answers (1)

chepner
chepner

Reputation: 532418

Just quote the variable expansion:

sed 's_3_'"$replace"'_g'

Upvotes: 2

Related Questions