Output of “time” command in shell (bash)

Time:2021-4-14

preface

I believe we all know that time under Bash is a very useful command. It can time the execution of a script or a program, which is usually very convenient when roughly comparing the execution efficiency of a program. However, you will find that the time text output by the time command cannot be simply redirected, for example, to a text file, which can only be displayed on the screen, which is very inconvenient for non interactive timing.

For example:


$ time find . -name "mysql.sh" >1.txt

real 0m0.081s
user 0m0.060s
sys  0m0.020s

$ time find . -name "mysql.sh" 2>2.txt
./work186/sms/bin/mysql.sh
./work186/sms/src/scripts/mysql.sh
./work186/sms/src/scripts1/mysql.sh
./work186/sms1/bin/mysql.sh
./work186/sms1/src/scripts/mysql.sh
./temp/sms/bin/mysql.sh
./temp/sms/src/scripts/mysql.sh

real 0m0.068s
user 0m0.040s
sys  0m0.030s

Through the above attempt, it is found that the output information of time cannot be redirected to the file. Why? Because time is the key word of the shell, the shell does special processing. It will process the command line following the time command as a whole. When redirecting, it is actually for the following commands, and the output of the time command itself will not be redirected.

The keyword time sets a tag, and you knowcommand(find)After the command is executed, the timing information is printed into stderr. The time keyword requires the entire command and pipeline, as well as the related redirection. That’s why simple redirection doesn’t work for time. This is defined by bash syntax. Redirection after command is a part of command for time.

Note:The output of the time command is in stderr

When the time command is executed, the command runs in the shell next to the current shell (that is, the shell executed by the time command), and the output of time itself is in the stderr of the current shell. As shown above, redirection only causes the stdout of command to be redirected to a text file instead of outputting the output of time itself.

The first solution is to put the time command and the command line to be executed into a shell code block, that is, a pair of braces. Pay attention to the use of spaces and semicolons.


$ { time find . -name "mysql.sh"; } 2>2.txt
./work186/sms/bin/mysql.sh
./work186/sms/src/scripts/mysql.sh
./work186/sms/src/scripts1/mysql.sh
./work186/sms1/bin/mysql.sh
./work186/sms1/src/scripts/mysql.sh
./temp/sms/bin/mysql.sh
./temp/sms/src/scripts/mysql.sh
$ cat 2.txt

real 0m0.068s
user 0m0.030s
sys  0m0.040s

The first way has been successful, which is to sum up { time command-line; } 2>file  Pay attention to the use of separators.

Another way is to use subshell

As follows:


$ (time find . -name "mysql.sh") 2>2.txt
./work186/sms/bin/mysql.sh
./work186/sms/src/scripts/mysql.sh
./work186/sms/src/scripts1/mysql.sh
./work186/sms1/bin/mysql.sh
./work186/sms1/src/scripts/mysql.sh
./temp/sms/bin/mysql.sh
./temp/sms/src/scripts/mysql.sh
$ cat 2.txt

real 0m0.083s
user 0m0.040s
sys  0m0.020s
[[email protected] root]#

The second way has also been successful. To sum up, it is(time command-line) 2>file Here, time is next to the parentheses (it’s OK, and the command line doesn’t have to end with a semicolon). Of course, it’s better to use the first method. After all, starting a subshell requires more resources.

summary

The above is the whole content of this article, I hope the content of this article can bring some help to your study or work, if you have any questions, you can leave a message to exchange.

Recommended Today

Large scale distributed storage system: Principle Analysis and architecture practice.pdf

Focus on “Java back end technology stack” Reply to “interview” for full interview information Distributed storage system, which stores data in multiple independent devices. Traditional network storage system uses centralized storage server to store all data. Storage server becomes the bottleneck of system performance and the focus of reliability and security, which can not meet […]