I know this is an old thread, but I got this problem now
thank you (me too) for the answer is working ...
just comment about mechanics:
to "see" why is not working when you don't use eval or bash -c (this asume that you can write in current directory ...)
echo "z='ls -1 | wc -l' ; "'$z' > z ; bash -x z
+ z='ls -1 | wc -l'
+ ls -1 '|' wc -l
ls: cannot access |: No such file or directory
ls: cannot access wc: No such file or directory
as you can see (on 3th line), when string is executed the pipe character will be surrounded by ' characters and that remove it special (pipe) signification
(long live bash for doing that ...)
another solution (less elegant, but who knows, someone may find it better) is:
z='ls -1 | wc -l' ; bash -c "$z"