0 votes
in Programming by (1.9k points)

I am writing a shell script where I input a value and want to use the value for some other commands. My problem is that I want to escape this value.

For example, if I input http://example.com in the following script

echo "Input a value for my_value"
read my_value

echo $my_value

It will result in http://example.com

But the result I wish is http\:\/\/example\.com

How do I achieve this?


The command I'm trying to run is

sed -i s/url/$my_value/g somefile.html

without the escape it becomes sed -i s/url/http://example.com/g somefile.html which obviously is a syntax error..

1 Answer

0 votes
by (2.4k points)

You can use other characters to split the s arguments. I like the ,

sed -i 's,url,http://example.com,g'

. If you really want it you can use sed to replace the / in the argument, before executing

url=$(echo "http://example.com"|sed 's,/,\\/,g')
sed -i 's/url/'"$url"'/g' input

131 questions

117 answers

7 comments

378 users

Welcome to Todayask Q&A, where you can ask questions and receive answers from other members of the community.
...