Can I use sed to manipulate a variable in bash?

Try this:

website=$(sed 's"https://stackoverflow.com/"\\/|g' <<< $website)

Bash actually supports this sort of replacement natively:

${parameter/pattern/string} — replace the first match of pattern with string.
${parameter//pattern/string} — replace all matches of pattern with string.

Therefore you can do:

website=${website////\\/}

Explanation:

website=${website // / / \\/}
                  ^  ^ ^  ^
                  |  | |  |
                  |  | |  string, '\' needs to be backslashed
                  |  | delimiter
                  |  pattern
                  replace globally

Leave a Comment