I'm having problems constructing regular expression from array of strings, for use in sed command.
Here's an example
#!/usr/bin/env bash
function join { local IFS="$1"; shift; echo "$*"; }
array=("foo" "bar" "baz")
regex=$(join "\|" "${array[@]}") # foo|bar|baz
echo "foo bar baz" | sed "s/${regex}/replaced/g"
I'm expecting this to output replaced replaced replaced, but it's returning foo bar baz since this regex is not hitting anything. The pipes in the regex are not being escaped correctly here.
What am I doing wrong?