简体   繁体   中英

sed and the odyssey of escaping a bash variable

Having

DEST_PATH='/var/www/clones'
site='xyz.com'

sed -i -e "s/\$log_path\s=\s'\(.*\)'/\$log_path = '$DEST_PATH\/$site\/logs'/" $DEST_PATH/$site/configuration.php

The problem is the forward slashes in first variable, because this is what is being processed and returns error:

sed -i -e "s/\$log_path\s=\s'\(.*\)'/\$log_path = '/var/www/clones\/xyz.com\/logs'/"  configuration.php

When this is what actually should be run:

sed -i -e "s/\$log_path\s=\s'\(.*\)'/\$log_path = '\/var\/www\/clones\/xyz.com\/logs'/"  configuration.php

So I know, I could replace all the / inside $DEST_PATH , and run the sed again, but I was wondering if you know or can think of any other/better way of doing so. Ideally, maybe having sed automatically escape the '$DEST_PATH/$site/logs' if possible.

Are you using a modern enough version of sed (eg, GNU sed )? Then you are not required to use / to separate pattern and substitution. Any character will do.

Eg, you can use

s,pattern,substitution,

instead of

s/pattern/substitution/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM