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?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityI 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?
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[@]}"