Can I use sed to manipulate a variable in bash?

Viewed 116641

In my program, I would like to first get the user input, and insert a \ before each / so I write this, but it doesn't work.

echo "input a website"
read website

sed '/\//i\/' $website
3 Answers

You can also use Parameter-Expansion to replace sub-strings in variable. For example:

website="https://stackoverflow.com/a/58899829/658497"
echo "${website//\//\\/}"

https:\/\/stackoverflow.com\/a\/58899829\/658497

Related