1

I have this script:

num='[0-9]'
sedcmd='-e "s/${num}/as df/g"'
echo 123 | sed -r $sedcmd

The last line produces this:

sed: -e expression #1, char 1: unknown command: `"'

What did I miss?

1 Answer 1

1

Do not use a variable to store shell commands, use an array. See BashFAQ-50 I'm trying to put a command in a variable, but the complex cases always fail.

You just use an array as below

num='[0-9]'
argArray=('-e' "s/${num}/as df/g")

and double-quote the array expansion to not let the words be split by Word-splitting and call as

echo 123 | sed -r "${argArray[@]}"

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.