bash - sed command creation via shell script - quotes and sed -


i'm implementing template renderer in shell script. template variables represented in template @var_name@ , values defined in separate shell script.

sample code:

# template variables values contact_email="myemail" contact_name="myname" vars="contact_email contact_name"  template_filepath="mytemplate.txt"  # template renderer set -x sedargs= var in $vars;      sedargs+=" -e \"s,@$var@,${!var},\"" done sed -r $sedargs $template_filepath 

sed command executed shell , printed because of "set -x":

+ sed -r -e '"s,@contact_email@,myemail,"' -e '"s,@contact_name@,myname,"' mytemplate.txt  

sed output:

sed: -e expression #1, char 1: unknown command: `"' 

i know single quotes around each sed expression causing non-intuitive error message, not know why added.

what wrong?

you have embedded quotes inside sedargs variable. these not removed when command executed. remove them, need call interpreter again, can using eval. example:

eval sed -r $sedargs $template_filepath 

you may need play around more (adding quotes, etc.).


Comments

Popular posts from this blog

PHP DOM loadHTML() method unusual warning -

python - How to create jsonb index using GIN on SQLAlchemy? -

c# - TransactionScope not rolling back although no complete() is called -