POSIX says this:
The times utility shall write the accumulated user and system times for the shell and for all of its child processes...
It's not obvious to me how it should behave in a subshell, and it's entirely possible that POSIX tried to document the ksh(88) behavior.
Nevertheless, in all other shells I have access to, a new subshell always resets the output of times to 0, while in ksh93[u+m] it accumulates the "main" shell even in a subshell.
For instance, this prints the "shell user seconds" component of the times output in a loop, in a subshell, and it prints ever-increasing numbers in ksh93 variants, but always 0-ish in all other shells.
ksh -c 'while x=$(times); do echo ${x%% *}; done'
0m00.003s
...
...
0m00.004s
0m00.004s
...
...
0m00.292s
0m00.292s
...
...
0m00.293s
0m00.293s
...
...
I'm intentionally not declaring it as a bug, but if compatibility with other shells is desirable for times, then it might be considered as such.
Cases where times can be useful is to measure execution duration of some function or shell snippet.
In all other shells I tried it can simply be x=$(benchmark ...; times), but in ksh93 and derivatives one would need to grab the output of times before and after running benchmark, convert them to some numeric form, and subtract the former from the latter. Or use some other methods (which do exist).
POSIX says this:
It's not obvious to me how it should behave in a subshell, and it's entirely possible that POSIX tried to document the ksh(88) behavior.
Nevertheless, in all other shells I have access to, a new subshell always resets the output of
timesto 0, while in ksh93[u+m] it accumulates the "main" shell even in a subshell.For instance, this prints the "shell user seconds" component of the
timesoutput in a loop, in a subshell, and it prints ever-increasing numbers in ksh93 variants, but always 0-ish in all other shells.I'm intentionally not declaring it as a bug, but if compatibility with other shells is desirable for
times, then it might be considered as such.Cases where
timescan be useful is to measure execution duration of some function or shell snippet.In all other shells I tried it can simply be
x=$(benchmark ...; times), but in ksh93 and derivatives one would need to grab the output oftimesbefore and after running benchmark, convert them to some numeric form, and subtract the former from the latter. Or use some other methods (which do exist).