I need to store some sed expressions in a variable and substitute it in the sed command.
myArgs="-e 's/foo/bar/' -e 's/foo2/bar2/'"
So far these are not working:
echo "foo" | sed ${myArgs}
# sed: -e expression #1, char 1: unknown command: `''
echo "foo" | sed $(echo ${myArgs})
# sed: -e expression #1, char 1: unknown command: `''
echo ${myArgs}
's/foo/bar/' -e 's/foo2/bar2/'
# 1st -e missing
Parameter expansion interprets the first -e argument in the variable.
-e may be interpreted as a test for exist, or argument to echo.
How can I prevent this and use it literally for the sed command?
To clarify, this is just a simplified version of a command that will contain a large number of sed expressions with characters that require escaping in the regex, and I would rather keep the expressions quoted as-is, and dump all the sed expressions into the sed command as a single variable.
If that's not possible, I'll use a different method such as the current answers below, however it seems there should be some built in bash method to not pre-expand the variable, and instead just interpret it as a string, before it's used in the final sed command.
The actual arguments I'm using will look something like this:
-e 's/^>chr1.*/>NC_032089.1_chr1/' -e 's/^>chr2.*/>NC_032090.1_chr2/'
With unknown strings