111
111

Reputation: 1918

How to store sed arguments in variable for parameter substitution in BASH

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

Upvotes: 1

Views: 580

Answers (2)

agc
agc

Reputation: 8406

Since we know the input, and the sed code is safe enough, it's OK to use eval here:

echo foo | eval sed $myArgs

Output:

bar

Related Q. about things to be wary of: Why should eval be avoided in Bash, and what should I use instead?

Mo Budlong's Sunworld column Command line psychology 101 is the most helpful thing I've seen for understanding command line parsing.

Upvotes: 1

Poshi
Poshi

Reputation: 5762

Don't use a string, use an array:

myArgs=("-e" "s/foo/bar/" "-e" "s/foo2/bar2/")
sed "${myArgs[@]}" <<< "foo"

Upvotes: 4

Related Questions