Alex
Alex

Reputation: 7688

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.

Upvotes: 0

Views: 47

Answers (1)

Roberto Reale
Roberto Reale

Reputation: 4317

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

E.g., you can use

s,pattern,substitution,

instead of

s/pattern/substitution/

Upvotes: 3

Related Questions