bug-bash
[Top][All Lists]
Advanced

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

Re: Command not found with builtins and variables


From: Bob Proulx
Subject: Re: Command not found with builtins and variables
Date: Wed, 10 Oct 2018 16:52:30 -0600
User-agent: Mutt/1.10.1 (2018-07-13)

Chet Ramey wrote:
> bugs@feusi.co wrote:
> > The thing I noticed is that when setting a variable and then running the
> > command "time", bash reports "command not found". For example, the
> > command:
> > 
> > TEST=1 time ls
> > 
> > Results in "bash: time: command not found"

That would work if you installed the GNU time package.

  https://www.gnu.org/software/time/

And it would produce the portable output format in both cases with the
time -p option, which is available both in the internal time and the
external GNU time command.  Otherwise GNU time has its own unique and
different default output format.

  $ TEST=1 time -p sleep 1
  real 1.00
  user 0.00
  sys 0.00

But regardless of the external standalone time program...  Order when
using the internal time here matters.  Since 'time' is special it
should always be first.  Put any variables to be set on the line
before the command by putting it before the command but after the
'time' word.

  $ time TEST=1 sleep 1
  real    0m1.003s
  user    0m0.000s
  sys     0m0.000s

> You don't have a command named `time' in a directory in your $PATH.

Well...  *They* don't have a time program in PATH.  But they could
have if they installed it.  And many people do.  :-)

> Bash doesn't have a `time' builtin; it has a `time' reserved word.
> Preceding `time' with a variable assignment removes its special meaning
> and causes it to not be recognized as a reserved word.

I might also mention that the bash 'time' needs this special status
because it times the entire pipeline.

  $ time sleep 1 | sleep 2
  real    0m2.004s
  user    0m0.003s
  sys     0m0.002s

Therefore conversely if someone is using the external time it might
give them an unexpected result.  Since the external time will not know
anything about a pipeline.  So this might catch people off guard too.

  $ TEST=foo time -p sleep 1 | sleep 2
  real 1.00
  user 0.00
  sys 0.00

When used it is best to always put 'time' first on the command line,
variables second, and commands after that.  Order matters[1].

Bob

[1] OT: In English the order also matters and it is always
opinion, size, age, shape, color, origin, material, purpose, noun.
Using any other order and it sounds really odd!  Reference
"The Elements Of Eloquence: How To Turn The Perfect English Phrase."
by Mark Forsyth.
http://www.npr.org/2016/09/08/493157931/bbc-editor-highlights-often-overlooked-english-language-rule



reply via email to

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