I have a problem with executing some of the commands using bash. Let's say I have a simple bash script:
#!/bin/bash
cmd="sh -c \"ls\""
$cmd
This works all fine. However, if I alter the argument for the command to contain space:
#!/bin/bash
cmd="sh -c \"ls -a\""
$cmd
then it throws -a": 1: -a": Syntax error: Unterminated quoted string error. It seems that bash correctly noticed open quote, but it stops looking for closing quote on a first space. If I change $cmd to echo $cmd it returns sh -c "ls -a" as expected. If I execute this command in terminal everything works, but $(./mysh.sh) fails with same error.
I have tried escaping the space with \, or 'double-escape' quotes with \\\" (silly idea, but was worth a try). Could anyone explain what is happening here?