I'm trying to create a shell script that concatenates multiple commands to a string and then execute the commands.
The following succeeds (Creates the 'y' and 'z' files and prints ls output):
$ /bin/touch /z && ls && /bin/touch /y
But the following fails (Creates the 'y' and 'z' files, but also a '&&' and 'ls' files)
$ A="/bin/touch /z && ls && /bin/touch /y"
$ $A
It seems that by executing the line using $A
the binary touch
gets the rest of the string as parameters and the &&
operator does not execute as intended.
(I also tried switching &&
with ;
, ||
and such but got the same result)
I found out the eval $A
does the trick, but I'm still curious as to why this happens. (And possibly want to skip the need to eval)
Thanks!