In the first version, the double quotes protect the space in the exec parameter from being interpreted by the shell, so exec
sees a single word with a space in it, "ls -lt". There isn't a program of that name, so it fails. In the second version, the shell sees the space.
You don't need the space in the value of arg
, because in the second version you have a space between $cmd
and $arg
anyway, but it doesn't do any harm. You don't actually need any quotes at all in this simple case, but if you are in the habit of including them, you won't forget them when you need them.