[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] Why time does not take environment variable?
From: |
Bob Proulx |
Subject: |
Re: [Help-bash] Why time does not take environment variable? |
Date: |
Mon, 26 Nov 2018 12:56:44 -0700 |
User-agent: |
Mutt/1.10.1 (2018-07-13) |
Peng Yu wrote:
> I thought that the first TIMEFORMAT set the environment variable for
> `time` temporarily. But it apparently has no effect. Could anyone help
> me understand why it is so? Thanks.
>
> TIMEFORMAT=%3R time sleep 1
> 1.00 real 0.00 user 0.00 sys
> export TIMEFORMAT=%3R
> time sleep 1
> 1.018
Inside bash 'time' is a shell keyword. Outside of bash 'time' is a
standalone utility.
$ type -a time
time is a shell keyword
time is /usr/bin/time
Why two? Because in bash 'time' will time the full command pipeline.
An external command cannot do that and would only be able to time one
command at a time. One would need to construct a time sh -c 'A | B'
type of thing in order to have the external 'time' work on a single
command line command. That is why 'time' is a shell keyword. It
needs to be special in order to time a pipeline of commands.
Therefore the shell syntax to set a variable for just one command
cannot be used before 'time' to set it. Doing so causes it to invoke
the external command.
You can always use the external GNU time command.
$ env TIME=%e time sleep 2.35
2.35
Bob