Skip to content
Advertisement

How can GNU time utility ignores pipe symbol and takes all what left in command line as one execve argument?

Linux, if I run time tar -c stock_file | lz4 > stock_file.lz4, actually it makes something like time (tar -c stock_file | lz4 > stock_file.lz4), but if I write one little program:

JavaScript

Then I build it to pretending_time and run it:

JavaScript

Well well, the parameter is well not well | cat, then I have the check the source code of time to figure it out how time takes well | cat as one parameter

JavaScript

Only one file source code, feels good

JavaScript

Feels not good, compile it carefully

JavaScript

What is

JavaScript

I have never seen it before, and I can’t compile it, is this allowed in c-lang? is this the way how time takes the pipe symbol | in the parameter?

EDIT after wondering solved

There is one builtin command time in my shell, but which will find the standalone time utility, you can if there is builtin by

JavaScript

Advertisement

Answer

The reason time behaves differently than other (external) programs is because time is not actually a program, it’s a bash builtin. For comparison, consider the following examples: with the bash builtin we get

JavaScript

whereas with the external time

JavaScript

So as you can see, in first case time times both sleep operations, whereas in the second case it only times the first sleep. In general, it is impossible to build a version of time as an external program that works like the first example. The reason is that whenever bash see the pipe character, it will execute the stuff on the left and the stuff on the right independently. So the left side will never have a chance to see what the right side command looks like. On the other hand, with the builtin time, bash can recognize it and treat it specially.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement