help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] Why time does not take environment variable?


From: Peng Yu
Subject: Re: [Help-bash] Why time does not take environment variable?
Date: Mon, 26 Nov 2018 14:18:31 -0600

On Mon, Nov 26, 2018 at 1:56 PM Bob Proulx <address@hidden> wrote:
>
> 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

It seems that `time` is special in the aspect of whether an inline
environment variable can be set or not. Is it documented somewhere in
the manual? Thanks.

$ cat help
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

declare -p BASH_SOURCE
$ cat  time
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

declare -p BASH_SOURCE
$ cat ./main.sh
#!/usr/bin/env bash
# vim: set noexpandtab tabstop=2:

set -v
X=10 help | head -n 1
TIMEFORMAT=%R time sleep 1

export PATH=".:$PATH"
X=10 help | head -n 1
TIMEFORMAT=%R time sleep 1
$ ./main.sh
X=10 help | head -n 1
GNU bash, version 4.4.19(1)-release (x86_64-apple-darwin17.3.0)
TIMEFORMAT=%R time sleep 1
        1.01 real         0.00 user         0.00 sys

export PATH=".:$PATH"
X=10 help | head -n 1
GNU bash, version 4.4.19(1)-release (x86_64-apple-darwin17.3.0)
TIMEFORMAT=%R time sleep 1
declare -a BASH_SOURCE=([0]="./time")

-- 
Regards,
Peng



reply via email to

[Prev in Thread] Current Thread [Next in Thread]