Try the following:
s=$({ time dd ... > /dev/null;} 2>&1)
Chepners answer gave me inspiration for the arguably better:
s=$(exec 2>&1; time dd ... > /dev/null)
$()
is already a subshell, so no need to create an extra subshell. Putting exec 2>&1;
before the command redirects the stdout of the entire subshell, putting it after the command would make it part of the command to time
and hence is applied only to the command passed to time
. Omitting the exec
and semicolon will try to execute the system time
binary intead of the builtin, and error out if that does not exist. Omitting just the exec
but keeping the semicolon will not work as that would cause only newly forked processes to be redirected and the time builtin will not be a new process.
Oh and by the way, the > /dev/null
does not dump the output of time
itself to /dev/null because it too is part of the command that time
executes and is not applied to time itself.