Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get values from 'time' command via bash script [duplicate]

I want to run some executables with the time command

time myexec -args

How can I store only the time output to a variable in bash? Thats the only part I care about for this script, not the output of the executable. Is there a way to get that value, or will I have to parse the text of the entire command?

like image 931
Derek Avatar asked Jan 06 '11 17:01

Derek


People also ask

How do you time a bash script?

Using Bash Shell's TIMEFORMAT The TIMEFORMAT is a string format that will be printed after the execution of the block code inside the time{} wrapper finishes. The %R specifies to print the elapsed time in seconds with milliseconds precision. Let's test our script: $ ./elapsed_time.sh It took 12.008 seconds.

What is Echo $$ in bash?

The echo command is used to display a line of text that is passed in as an argument. This is a bash command that is mostly used in shell scripts to output status to the screen or to a file.

What does $() mean in bash?

Again, $() is a command substitution which means that it “reassigns the output of a command or even multiple commands; it literally plugs the command output into another context” (Source).

What is $@ in bash script?

bash [filename] runs the commands saved in a file. $@ refers to all of a shell script's command-line arguments. $1 , $2 , etc., refer to the first command-line argument, the second command-line argument, etc. Place variables in quotes if the values might have spaces in them.


1 Answers

See BashFAQ/032.

All output (stdout, stderr and time) captured in a variable:

var=$( { time myexec -args; } 2>&1 )

Output to stdout and stderr go to their normal places:

exec 3>&1 4>&2
var=$( { time myexec -args 1>&3 2>&4; } 2>&1 )  # Captures time only.
exec 3>&- 4>&-
like image 89
Dennis Williamson Avatar answered Sep 29 '22 16:09

Dennis Williamson