Why BSD eval cannot run gtime

bugcommand linehomebrewunix

I am considering this answer with GNU time which I installed by brew.

data.txt

hello 
amigo
this line 3
and here we go 4
and 5 is here

Code in BSD time

time eval $(echo sed $(for i in 1 2 3 5; do echo "-e ${i}p"; done) -n data.txt )
hello 
amigo
this line 3
and 5 is her

real    0m0.008s
user    0m0.003s
sys 0m0.005s

Code in GNU time

$ gtime eval $(echo sed $(for i in 1 2 3 5; do echo "-e ${i}p"; done) -n data.txt )
gtime: cannot run eval: No such file or directory
Command exited with non-zero status 127
0.00user 0.00system 0:00.00elapsed ?%CPU (0avgtext+0avgdata 1228800maxresident)k
0inputs+0outputs (0major+93minor)pagefaults 0swaps

and with quotes

gtime eval '$(echo gsed $(for i in 1 2 3 5; do echo "-e ${i}p"; done) -n data.txt )'
gtime: cannot run eval: No such file or directory
Command exited with non-zero status 127
0.00user 0.00system 0:00.00elapsed 50%CPU (0avgtext+0avgdata 1196032maxresident)k
7inputs+0outputs (0major+91minor)pagefaults 0swaps

Based on Matthieu's comment, the gtime is looking for a eval bin but it is built-in bash command. Command which eval gives nothing.

Why GNU time cannot run eval?

Best Answer

gtime can't execute/time commands definied internally by the shell. Commands are executed using the exec() system call, but the system call basically only knows about commands with binaries (e.g. in /usr/bin). Builtin commands are included in the shell binary, there is no way for exec() to execute them.

You need to either find a way to execute the command without relying on shell builtins or use the internal time command of your shell. One option might (should work, but I have no means of testing it right now) be to run a new shell within gtime

gtime bash -c 'eval $(echo sed $(for i in 1 2 3 5; do echo "-e ${i}p"; done) -n data.txt)'

The timing will be slightly off because the time required to run a new shell is included, but you can measure that seperately by running gtime bash -c ':'.