I have been experimenting with some bash scripting and ran into a problem that I do not quite understand. I am trying to print out all my files in a directory and use the print out to show not only the name of the file, but also the size and last modified date. I can do that with the following command:
ls | xargs -I '{}' sh -c 'echo "{}" $(stat -c "%y %s" "{}")'
I got the the use of sh -c
from this post: xargs with multiple commands (first answer). I understand that the sh -c
part is the command that's being executed, which reads in the string as a command. However, what I don't quite understand is why other commands won't work, such as eval
in place of sh -c
. For example:
ls | xargs -I '{}' eval 'echo "{}" $(stat -c "%y %s' "{}")'
Doesn't eval
also take a string as an argument and then evaluate it? (eval command in bash) I get that I have a working command with the sh -c
, but I would like to know why this command is different from eval
.