sid
sid

Reputation: 925

Escape backslash in variable

I am using sed to replace url's in a file, everything works fine just a hiccup when the url contains a '\'

exmaple url: http**://www.example.com/simi/icr

# variables
ICR_KEY=somekey
ICR_KEY_VAL="http\://www.example.com/simi/icr"
sed "s!${ICR_KEY}=.*!${ICR_KEY}=${ICR_KEY_VAL}!" properties > tmp

This replaces the URL, but the output does not contain the backslash from the variable value.

Upvotes: 3

Views: 19675

Answers (1)

Philipp
Philipp

Reputation: 49802

Both bash and sed interpret the backslash as escape character. Use single quotes to prevent this for bash, and double the backslash for sed:

ICR_KEY_VAL='http\\://www.example.com/simi/icr'

Upvotes: 1

Related Questions