Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux sed replace a string - Can someone explain this error?

I have 1 file and hope use sed to replace the string.

a.txt

#define 1
#define 2

b.sh

#!/bin/bash
PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin
export PATH

MODEL_NAME='123 %s abc 3.0 %d <<456: %s>>'
sed -i 's/#define 1/#define 1 \"'${MODEL_NAME}'\"/g' 1.txt

My expect result in 1.txt:

#define 1 "123 %s abc 3.0 %d <<456: %s>>"
#define 2

But I got the error message from console:

sed: -e expression #1, char 27: unterminated `s' command

Could someone help explain the condition for this? thanks.

like image 836
user3535459 Avatar asked Feb 22 '26 06:02

user3535459


1 Answers

Because you are using single quotes around your sed command, the shell variable ${MODEL_NAME} never gets expanded. Try:

sed -i "s/#define 1/#define 1 \"${MODEL_NAME}\"/g" 1.txt
like image 121
Josh Jolly Avatar answered Feb 23 '26 20:02

Josh Jolly



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!