Without that concatenation
cmdOutput=$( eval "$cmd" )
would work but
cmdOutput=$( eval $cmd )
would not if $cmd
contains spaces. And while both commands will usually produce the same output, the first one creates a temporary string that is fed as single argument to eval
and then destroyed again while the second one simply does not.
However, this can have an impact in certain corner cases, which is why I spoke of "usually" above:
ls_date=$( printf '%s\n%s' ls main.bash )
echo "1: $( eval $ls_date -l )"
echo
echo "2: $( eval "$ls_date -l" )"
Output:
1: -rwxrwxrwx 1 root root 112 Jan 6 23:31 main.bash
main.bash: line 5: main.bash: command not found
2: main.bash
In the first case, main.bash
and -l
are both treated as arguments to ls
. In the second case ls
and main.bash
are treated as separate commands and -l
would be an argument to the second command (yet main.bash
was had no x-bit set and also was not in the path).
Last but not least: Syntax parser will try to parse unquoted syntax following eval
but they typically won't parse any syntax in quotes except for $-expressions ($var
, $(cmd)
, and $((<math>))
).