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

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[@]}"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.