3

this works for me:

CMD="ls -la"
$CMD

while this always fails:

CMD="sed -e 's|abc|abc|g' file"
$CMD

with the error message:

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

That's because when you enter the command on the command line, shell runs "expansions" on it. The last step is "Quote Removal", but it only removes quotes that weren't a result of any expansion. (See EXPANSIONS in man bash for details.) Here, the quotes are part of the variable expansion, so they're not removed.

You can enter the substitution without quotes to make it work:

cmd='sed -e s|abc|abc|g file'
$cmd

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.